PHP code example of codex-team / hawk.laravel

1. Go to this page and download the library: Download codex-team/hawk.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/ */

    

codex-team / hawk.laravel example snippets




use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        //
    })
    ->withExceptions(function (Exceptions $exceptions) {
        HawkBundle\Integration::handles($exceptions);
    })
    ->create();

'providers' => [
    // Other service providers...
    HawkBundle\ErrorLoggerServiceProvider::class,
],

app(\HawkBundle\Catcher::class)->setUser([
    'name' => 'John Doe',
    'photo' => 'https://example.com/avatar.png',
]);

app(\HawkBundle\Catcher::class)->setContext([
    'page' => 'checkout',
    'cart_id' => 123,
]);

use HawkBundle\Catcher;

class TestController extends Controller
{
    private Catcher $catcher;

    public function __construct(Catcher $catcher)
    {
        $this->catcher = $catcher;
    }

    public function test()
    {
        try {
            // Code that may fail
        } catch (\Exception $e) {
            $this->catcher->sendException($e);
        }
    }
}

app(\HawkBundle\Catcher::class)->sendMessage(
    'Checkout started',
    [
        'cart_id' => 123,
        'step' => 'payment',
    ]
);



namespace App\Hawk;

use Hawk\EventPayload;
use HawkBundle\Services\BeforeSendServiceInterface;

class BeforeSendService implements BeforeSendServiceInterface
{
    public function __invoke(EventPayload $eventPayload): ?EventPayload
    {
        $user = $eventPayload->getUser();

        // Remove sensitive data
        if (!empty($user['email'])) {
            unset($user['email']);
            $eventPayload->setUser($user);
        }

        // Skip sending in specific cases
        if ($eventPayload->getContext()['skip_sending'] ?? false) {
            return null;
        }

        return $eventPayload;
    }
}

return [
    'integration_token' => env('HAWK_TOKEN'),
    'before_send_service' => \App\Hawk\BeforeSendService::class,
];
bash
php artisan hawkbundle:publish