1. Go to this page and download the library: Download dantepiazza/laravel-base library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dantepiazza / laravel-base example snippets
// app/Http/Controllers/Controller.php (published stub)
namespace App\Http\Controllers;
use DantePiazza\LaravelApiResponse\ApiResponse;
abstract class Controller
{
public function __construct(protected ApiResponse $api) {}
}
class InvoiceController extends Controller
{
public function index(): JsonResponse
{
return $this->api->success('Invoices retrieved.')->data(Invoice::paginate(20))->response();
}
public function store(StoreInvoiceRequest $request): JsonResponse
{
return $this->api->created('Invoice created.')->data(Invoice::create($request->validated()))->response();
}
public function show(Invoice $invoice): JsonResponse
{
return $this->api->success()->data($invoice)->response();
}
public function destroy(Invoice $invoice): JsonResponse
{
$invoice->delete();
return $this->api->success('Invoice deleted.')->response();
}
}
$correlationId = $request->header('X-Correlation-ID');
// or
$correlationId = request()->header('X-Correlation-ID');
use DantePiazza\LaravelBase\Support\CacheLastUpdate;
// Read the current value (defaults to now() the first time it's read)
CacheLastUpdate::get();
// Bump it — call this from an observer/listener whenever data that SPA
// clients cache locally is created, updated or deleted
CacheLastUpdate::touch();
class CategoryObserver
{
public function saved(Category $category): void
{
CacheLastUpdate::touch();
}
}
use DantePiazza\LaravelBase\Http\Controllers\WebhookController;
Route::post('webhooks/{source}', [WebhookController::class, 'handle']);
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DantePiazza\LaravelBase\Http\Controllers\WebhookController as BaseWebhookController;
class ConvoyWebhookController extends BaseWebhookController
{
protected function verifySignature(Request $request, string $source): bool
{
return hash_equals(
hash_hmac('sha256', $request->getContent(), config('services.convoy.secret')),
(string) $request->header('X-Convoy-Signature')
);
}
}
use Illuminate\Support\Facades\Event;
// Single event
Event::listen('webhook.stripe.charge.succeeded', function (array $payload) {
$chargeId = $payload['data']['id'];
});
// Wildcard — all events from one source
Event::listen('webhook.convoy.*', ConvoyWebhookListener::class);
// Wildcard — every webhook regardless of source
Event::listen('webhook.*', GeneralWebhookListener::class);
[
'source' => 'stripe',
'event_type' => 'charge.succeeded',
'data' => [...], // $body['data'] if present, otherwise full body
'headers' => [...], // all request headers
'raw' => [...], // full decoded request body
]