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')->name('users.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');
use Laika\Route\Invoke;
Invoke::setResolver(fn (string $class): object => $container->make($class));
Url::get('/users/{id}', 'UserController@show');
Url::get('/users/{id:[0-9]+}', 'UserController@show'); // regex constraint
Url::get('/files/{path:.+}', 'FileController@show'); // catch-all, slashes
Url::get('/users/{id}', 'UserController@show')->name('users.show');
Url::url('users.show', ['id' => 5]); // "/users/5", a path only
Url::group('admin', function () {
Url::get('/dashboard', 'Admin\DashboardController@index'); // /admin/dashboard
Url::group('billing', function () {
Url::get('/invoices', 'Admin\Billing\InvoiceController@index'); // /admin/billing/invoices
});
});
use Laika\Route\Handler;
Handler::registerGroup('admin', function () {
Url::get('/dashboard', 'Admin\DashboardController@index');
Handler::registerGroup('billing', function () {
Url::get('/invoices', 'Admin\Billing\InvoiceController@index');
}, ['Permission|perm=billing.view']);
}, ['Authenticate'], ['LogAccess']);
// /admin/billing/invoices: Authenticate → Permission → controller → LogAccess
Url::get('/dashboard', 'DashboardController@index')->pipeline('Authenticate');
Url::get('/admin', 'AdminController@index')->pipeline(['Authenticate', 'Role|role=admin']);
Url::get('/admin', 'AdminController@index', ['Authenticate']); // third argument
Url::globalPipeline([\Laika\Shield\Pipeline\ShieldPipeline::class]); // every matched route
namespace App\Pipeline;
use Laika\Route\Contracts\PipelineInterface;
use Laika\Service\Response;
class Role implements PipelineInterface
{
public function handle(callable $next, array &$params): ?string
{
if (($_SESSION['role'] ?? null) !== ($params['role'] ?? null)) {
Response::setStatus(403);
return 'Forbidden';
}
return $next();
}
}
Url::get('/orders', 'OrderController@index')->filter(['LogAccess|level=info']);
Url::globalFilter(['LogResponse']);
namespace App\Filter;
use Laika\Route\Contracts\FilterInterface;
class LogAccess implements FilterInterface
{
public function terminate(callable $next, ?string $response, array &$params): ?string
{
error_log('level=' . ($params['level'] ?? 'default'));
return $next($response);
}
}
Url::fallback('admin', fn () => '<h1>Admin page not found</h1>'); // under /admin
Url::fallback(null, function () { // everything else
\Laika\Service\Response::setStatus(404);
return '<h1>Page not found</h1>';
});
Url::dispatch();