Wallogit.com
2017 © Pedro Peláez
Very fast request router
With this library you can easily implement routing in your PHP application, and it's insanely fast!, (*1)
To install with composer:, (*2)
composer require kito92/epicroute
Requires PHP 5.3+., (*3)
A simple file setup is located under 'example' directory. Here's a basic usage example:, (*4)
<?php
require 'vendor/autoload.php';
$route = \EpicRoute\Route::getInstance();
$route->get('/', function(){
echo 'response';
});
$route->dispatch();
Available methods are the following:, (*5)
$route->get('/...', function(){ /*...*/ });
$route->post('/...', function(){ /*...*/ });
$route->put('/...', function(){ /*...*/ });
$route->patch('/...', function(){ /*...*/ });
$route->delete('/...', function(){ /*...*/ });
Here's how you can match a variable:, (*6)
$route->get('/users/:name', function($name){
echo 'Welcome back ' . $name;
});
and in this way you can match only numeric value:, (*7)
$route->get('/users/:id{[0-9]*}', function($id){
echo 'user id ' . $id;
});
To match all the sub-URLs you can add the "+" at the end of a variable's name:, (*8)
$route->get('/:slug+', function($paths){
print_r($paths);
});
This will match /any/route/you/goes, and the variable $paths is an array which contains
the subfolder requested., (*9)
Remember that, for the same base url, if you have multiple regex route and a generic one, you HAVE to declare first those who have a regex, than the generic one, or the router will serve always the generic. You can call several methods through the fluent interface. So, for example:, (*10)
$route->get('/users/:id{[0-9]*}', function($id){ /*...*/ })
->get('/users/:name', function($name){ /*...*/ })
->dispatch();
You can define your custom middleware implementing the \EpicRoute\Middleware interface:, (*11)
class CustomMiddleware implements \EpicRoute\Middleware{
/**
* This method will be executed before the routing
*
* @return mixed
*/
function before(){
// TODO: Implement before() method.
}
/**
* This method will be executed after the routing
*
* @return mixed
*/
function after(){
// TODO: Implement after() method.
}
}
and you can associate to a route in this way:, (*12)
$route->get('/', function(){
echo 'home';
}, ['middleware' => CustomMiddleware::class]);
If you want to set a middleware to a group of routes, you can declare a group of view:, (*13)
$route->group(['middleware' => CustomMiddleware::class], function () use ($route){
$route->get('/users/:id{[0-9]*}', function ($id){
echo 'user id ' . $id;
});
$route->get('/users/:name', function ($name){
echo 'Welcome back ' . $name;
});
})->dispatch();
The MIT License (MIT). Please see License File for more information., (*14)