kenphp / router
Simple PHP Router
Requires
- php: >=7.0
Requires (Dev)
- codeception/codeception: ^2.3
This package is auto-updated.
Last update: 2024-10-15 03:51:10 UTC
README
A simple PHP Router for your web application. This library is part of KenPHP Project, but can be used independently.
Features
- Static Route Patterns
- Dynamic Route Patterns
- Named parameters
- Optional parameters
- Regex-based route patterns
- Subrouting
- Supports web application routing
- Before and After route middleware
- Custom handler when a route is not found
What it does ?
- Store route patterns, handlers, and middlewares information
- Resolve request to matching patterns
What it doesn't ?
- Parse route path from $_SERVER or any other means. You must provide the route path and method to
Router::resolve
method. - Execute middlewares and handlers. It only returns an array containing matched route handlers, middlewares, and parameters found in the request.
- Why isn't this library receives
Psr\Http\Message\RequestInterface
implementation and returnsPsr\Http\Message\ResponseInterface
?
This library aims to gives as much freedom as possible to the user. Not everyone are using PSR-7 implementation and we want to respect that.
Requirements
- PHP 7.0 or greater
Installation
The easiest way to install is using Composer
$ composer require kenphp/router
Methods
-
route($method, $route, $handler, $options = []) : void
Example :
$router->route('GET', '/users', ['UserController', 'listUsers']);
-
get($route, $handler, $options = []) : void
Example :
$router->get('/users/{id}', ['UserController', 'getUser']);
-
head($route, $handler, $options = []) : void
Example :
$router->head('/users', ['UserController', 'listUsers']);
-
post($route, $handler, $options = []) : void
Example :
$router->post('/users', ['UserController', 'createUser']);
-
put($route, $handler, $options = []) : void
Example :
$router->put('/users/{id}', ['UserController', 'updateUser']);
-
delete($route, $handler, $options = []) : void
Example :
$router->delete('/users/{id}', ['UserController', 'deleteUser']);
-
group($route, $fn, $options = []) : void
Example :
$router->group('/api', function() use ($router) { $router->get('/products/{id}', ['ProductController', 'getProduct']); });
-
setNotFoundHandler(callable $handler) : void
Example :
$router->setNotFoundHandler(function() { echo 'Page not found.'; });
-
resolve($requestRoute, $method) : null|array
This function would return an array containing the following keys :
handler
params
- Optional keys. This would be filled with any data from the
$options
parameter.
Example :
$router->get('/users/{id}', ['UserController', 'getUser'], [ 'namespace' => 'app\controllers' ]); $routeArray = $router->resolve('/users/1', 'GET'); /** * $routeArray would contains * [ * 'handler' => ['UserController', 'getUser'], * 'params' => ['id' => 1], * 'namespace' => 'app/controllers', * ] */