PHPRouter is a lightweight module that adds REST-like routing functionality to your project. It's inspired by ReactRouter and PHP Falcon framework.
- Include the following directive into your
.htaccess
file or the root config.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?s).*)$ index.php [QSA, L]
</IfModule>
- Initialize
Router
and map a route toGET
request. Try accessing/library/orwell/1984
.
require_once('PHPRouter/Router.php');
require_once('PHPRouter/RouteParser.php');
$router = new Router(new RouteParser());
$router->addGet('/library/{author}/{book}', function($author, $book) {
echo "You've requested '$book' by $author. Enjoy reading!";
});
$router->resolve();
- Initialize a
Router
with aRouteParser
(make your own parser or choose one of preset) - Set up
Route
s to map paths and requests to your custom callback. - Call
resolve()
method to makeRouter
resolve incoming requests.
PHProuter
maps a route schema to a custom callback. A route schema is a string, which describes a route structure. (i.e./blog/articles
).- Parametrized routes by default (i.e.
/user/{id}
). All parameters are retreived from matched paths and passed as the callback arguments. - Easily extendable.
Router
class fully relies on interfaces, so one can easily add new functionality by either extending one of supplied parsers, or writing a completely new one.
-
DefaultRouteParser
A naive parser implementation, which doesn't support route parameters and simply makes bijective schema-path mapping. -
RouteParser
A more advanced parser, which supports all listed features.
PHPRouter exposes three interfaces to the programmer: IRoute
, IRouter
, and IRouteParser
.
IRoute
/*
Returns the schema, which has been passed in the constructor
*/
function getSchema();
/*
Returns parameter names, which have been passed in the constructor
*/
function getParameterNames();
IRouter
/*
Adds a new route schema, which can later be mapped to
using `resolve` method. If a successful match is present, the `$callback`
will be invoked
*/
function add($routeSchema, Callable $callback);
/*
Adds a route schema, which will only be matched in case of GET request
*/
function addGet($routeSchema, Callable $callback);
/*
Adds a route schema, which will only be matched in case of POST request
*/
function addPost($routeSchema, Callable $callback);
/*
Adds a route schema, which will only be matched in case of PUT request
*/
function addPut($routeSchema, Callable $callback);
/*
Adds a route schema, which will only be matched in case of DELETE request
*/
function addDelete($routeSchema, Callable $callback);
/*
Tries to map the `$routeString` to a registered route schema.
If no arguments passed, router relies on incoming request super globals.
If a match found, a corresponding callback is invoked.
Otherwise, a NoRouteMatchException is thrown.
*/
function resolve($routeString);
IRouteParser
/*
Parses a $schema and returns a Route object.
In preset parsers the process includes:
1. Tokenization
2. Tokens validation
3. Parameter names extraction
*/
function parse($schema);
/*
Returns `true` if a $path matches $route's schema and `false`, otherwise.
This process relies solely on route schema stored inside the $route
*/
function matches(Route $route, $path);
/*
Returns parameters, which are extracted from $path.
This process relies solely on route schema stored inside the $route
*/
function extractParameters(Route $route, $path);