PHP code example of zerethonapp / flow-laravel

1. Go to this page and download the library: Download zerethonapp/flow-laravel 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/ */

    

zerethonapp / flow-laravel example snippets


// Explicitly enable or disable (null = auto-detect based on environment)
'enabled' => env('FLOW_ENABLED'),

// Environments where Flow should NOT run
'except_environments' => ['testing'],

// URI patterns to exclude from tracing
'except' => [
    'telescope*',
    'horizon*',
],

// Sample rate (0.0 - 1.0): trace only a percentage of requests
'sample_rate' => env('FLOW_SAMPLE_RATE', 1.0),

'sources' => [
    'request' => true,      // Request lifecycle
    'controller' => true,   // Controller execution
    'database' => true,     // Database queries
    'external' => true,     // External HTTP calls
],

'options' => [
    'database' => [
        'capture_sql' => false,      // Include SQL text in trace
        'capture_bindings' => false, // Include query bindings
    ],
],

'storage_path' => base_path('.zerethon/flow-history.json'),
'trace_directory' => storage_path('flow-traces'),
'max_records' => 1000,

Route::middleware(['flow.trace'])->group(function () {
    Route::get('/orders/{id}', [OrderController::class, 'show']);
});

use Zerethon\Flow\Laravel\Facades\Flow;

$order = Flow::traceService('OrderService.findOrder', function () use ($id) {
    return $this->orderService->findOrder($id);
});

$payload = Flow::traceExternal('BillingApi.charge', function () use ($order) {
    return Http::post('https://billing.example.com/charge', ['order_id' => $order->id])->json();
});

flow()->traceService('UserService.findUser', fn () => $service->findUser($id));

Flow::trace('service', 'UserService.findUser', fn () => $service->findUser($id));
Flow::trace('external', 'PaymentApi.charge', fn () => $api->charge($amount));

use Zerethon\Flow\Laravel\Support\Traceable;

class UserService
{
    use Traceable;

    public function findUser(int $id): User
    {
        return $this->traceService('UserService.findUser', function () use ($id) {
            // Business logic here
            return User::find($id);
        });
    }

    public function syncWithExternalApi(): void
    {
        $this->traceExternal('ExternalUserApi.sync', function () {
            // External API call
            Http::post('https://api.example.com/sync');
        });
    }
}

use Zerethon\Flow\Laravel\Support\Trace;

class OrderService
{
    #[Trace('service')]
    public function processOrder(int $orderId): void
    {
        // Automatically traced as "OrderService.processOrder"
    }

    #[Trace('external', 'PaymentAPI.charge')]
    public function chargePayment(float $amount): void
    {
        // Automatically traced with custom label
    }
}

// routes/web.php
Route::middleware(['flow.trace'])->get('/flow-demo', function () {
    $user = \Zerethon\Flow\Laravel\Helpers\Flow::trace('service', 'UserService.findUser', function () {
        return DB::table('users')->where('id', 1)->first();
    });

    $remote = \Zerethon\Flow\Laravel\Helpers\Flow::external('BillingApi.status', function () {
        return Http::get('https://httpbin.org/status/200')->status();
    });

    return response()->json(['user' => $user?->id, 'remote_status' => $remote]);
});
bash
php artisan flow:install
bash
php artisan vendor:publish --tag=flow-config