selami / router
This package is abandoned and no longer maintained.
No replacement package was suggested.
Router and Dispatcher built on top nikic/FastRoute that returns controller class name to be instantiated.
1.2.2
2019-03-03 11:34 UTC
Requires
- php: ^7.2
- nikic/fast-route: ^1.3
Requires (Dev)
- php-coveralls/php-coveralls: ^1.1
- phpstan/phpstan: ^0.10.6
- phpunit/phpcov: ^5.0
- phpunit/phpunit: ^7.5
- roave/security-advisories: dev-master
- squizlabs/php_codesniffer: ^3.3
- zendframework/zend-diactoros: ^1.8
README
Router and Dispatcher built on top of nikic/FastRoute that returns controller class name to be instantiated, desired content-type and route aliases if defined.
Installation
Install this library using composer:
$ composer require selami/router
Example
Get global values.
<?php declare(strict_types=1); $request = new PSR7ServerRequest(); // Let's say this class implements PSR7 ServerRequestInterface $response = new PSR7ServerResponse(); // Let's say this class implements PSR7 ResponseInterface $defaultReturnType = Selami\Router\Router::JSON; // Possible values: Selami\Router\Router::HTML, Selami\Router\Router::JSON, Selami\Router::TEXT, Selami\Router\Router::CUSTOM, Selami\Router\Router::REDIRECT, Selami\Router::DOWNLOAD. To be used to send output. $requestMethod = 'GET'; // i.e. $_SERVER['REQUEST_METHOD'] $requestedUri = '/user/12/inbox'; // i.e. $_SERVER['REQUEST_URI']
Create Selami\Router Instance.
$router = new Selami\Router( $defaultReturnType, $requestMethod, $requestedUri );
Add routes that expect HTTP request methods. $route variable uses nikic/FastRoute's route syntax.
use Selami\Router\Router; $route = '/'; $action = Controllers\Home::class; $returnType = Router::HTML; $alias = 'home'; $router->get( $route, // required $action, // required $returnType, // optional, default: $defaultReturnType $alias // optional, default null ); $router->post('/login', Controllers\Login::class, Router::REDIRECT); $router->get('/dashboard', Controllers\Dashboard::class, Router::HTML, 'dashboard'); $router->get('/api/user/{id}', Controllers\Api\Users::class, Router::JSON); $router->get('/user/{id:\d+}/{box}', Controllers\Api\Users\Inbox::class, Router::HTML, 'user_home');
Get requested route info and aliases.
$routeInfo = $router->getRoute(); // Returns Selami\Router\Route object.
Now you can call your controller.
$controller = new {$routeInfo->getController()}($routeInfo->getUriParameters()); $outputMethod = 'return' . ucfirst($routeInfo->getReturnType()); echo $controller->$outputMethod($request, $response);
Our sample Controller class can be like:
<?php declare(strict_types=1); namespace Controller\Api\Users; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use BaseController; class Inbox extends BaseController { private $uriParameters; public function __construct($uriParameters) { parent::__construct(); $this->uriParameters = $uriParameters; } public function returnHTML(ServerRequestInterface $request, ResponseInterface $response) { $response = $this->response->withHeader('Content-Type', 'text/html'); $response->getBody()->write('Your user id is: ' . $this->args['id'] . '. You are viewing your '. $this->uriParameters['box']); return $response->output(); } }