PHP code example of legatus / router
1. Go to this page and download the library: Download legatus/router 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/ */
legatus / router example snippets
declare(strict_types=1);
use Legatus\Http\Router;
use Legatus\Http\RoutingContext;
use Legatus\Http\RoutingContextError;
use Psr\Http\Message\ResponseInterface as Resp;
use Psr\Http\Message\ServerRequestInterface as Req;
use function Legatus\Http\handle_func;
/**
* @param Req $req
* @return Resp
* @throws RoutingContextError
*/
function show_user(Req $req): Resp {
$id = RoutingContext::of($req)->getParam('id');
return new Nyholm\Psr7\Response(200, [], 'Hello User ' . $id);
}
$router = new Router();
$router->get('/users/:id', handle_func('show_user'));
$request = new Nyholm\Psr7\ServerRequest('GET', '/users/1');
$response = $router->handle($request);
echo $response->getBody() . PHP_EOL; // Hello User 1