PHP code example of laikait / laika-route

1. Go to this page and download the library: Download laikait/laika-route 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/ */

    

laikait / laika-route example snippets


use Laika\Route\Url;

Url::get('/users', 'UserController@index');
Url::post('/users', 'UserController@store');
Url::put('/users/{id}', 'UserController@update');
Url::patch('/users/{id}', 'UserController@patch');
Url::delete('/users/{id}', 'UserController@destroy');
Url::options('/users', 'UserController@options');

Url::get('/users/{id}', 'UserController@show');
Url::get('/users/{id:\d+}', 'UserController@show'); // regex constraint

// Single
Url::get('/dashboard', 'DashboardController@index')->pipeline('Auth');

// Multiple
Url::get('/orders', 'OrderController@index')->pipeline(['Auth', 'VerifiedEmail']);

// With args
Url::get('/admin', 'AdminController@index')->pipeline(['Role|role=admin']);

// Multiple args
Url::get('/reports', 'ReportController@index')->pipeline(['Throttle|limit=60,window=60']);

// Global (applies to all routes)
Url::globalPipeline(['Csrf', 'Cors']);

namespace App\Pipeline;

use Laika\Route\Interfaces\PipelineInterface;

class Role implements PipelineInterface
{
    public function handle(callable $next, array &$params)
    {
        if (($_SESSION['role'] ?? null) !== ($params['role'] ?? null)) {
            http_response_code(403);
            return 'Forbidden'; // stops chain and print
        }
        // return $next(false); Stop pipelines chain and call controller
        return $next(); // continues chain
    }
}

// Single
Url::get('/orders', 'OrderController@index')->filter('LogAccess');

// Multiple
Url::get('/orders', 'OrderController@index')->filter(['LogAccess', 'CacheResponse']);

// With args
Url::get('/reports', 'ReportController@index')->filter(['LogAccess|level=info']);

// Global
Url::globalFilter(['LogResponse']);

namespace App\Filter;

use Laika\Route\Interfaces\FilterInterface;

class LogAccess implements FilterInterface
{
    public function terminate(callable $next, ?string $response, array &$params): ?string
    {
        error_log('level=' . ($params['level'] ?? 'default'));
    }
}

Url::get('/users/{id}', 'UserController@show')->name('users.show');

$url = Url::url('users.show', ['id' => 5]); // /users/5

// Basic
Url::group('admin', function () {
    Url::get('/dashboard', 'Admin\DashboardController@index');
});

// Nested (inherits parent's pipeline/filter)
Url::group('admin', function () {
    Url::group('billing', function () {
        Url::get('/invoices', 'Admin\Billing\InvoiceController@index');
    })->pipeline(['Permission|perm=billing.view']);
})->pipeline(['Auth']);

// Chained pipeline + filter (applied retroactively to all routes in group)
Url::group('api', function () {
    Url::post('/payments', 'PaymentController@store')->pipeline(['ApiKey']);
})->pipeline(['Cors'])->filter(['LogApi']);

// Per group prefix
Url::fallback('admin', fn() => '<h1>Admin route not found</h1>');

// Default (no prefix)
Url::fallback(null, fn() => '<h1>Page not found</h1>');

Url::dispatch();