Wallogit.com
2017 © Pedro Peláez
Fast router for php with regex, middleware route separator support
_________ __________ __
/ _____/ ____ ____ \______ \ ____ __ ___/ |_ ___________
\_____ \_/ __ \ / \ ______ | _// _ \| | \ __\/ __ \_ __ \
/ \ ___/| | \ /_____/ | | ( <_> ) | /| | \ ___/| | \/
/_______ /\___ >___| / |____|_ /\____/|____/ |__| \___ >__|
\/ \/ \/ \/ \/
------------------------------------------------------------------------------
Click to see SenRouter in Packagist, (*1)
composer require touskar/sen-router, (*2)
``` .htaccess SetEnv FRONTAL_CONTROLER index.php SetEnv FRONTAL_CONTROLER_SUB_DIR /web/root/ # or / for non subdired project, (*3)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . %{ENV:FRONTAL_CONTROLER} [L]
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
, (*5)
#### index.php ```php require_once('../vendor/autoload.php'); use SenRouter\Http\Dispatcher\Router; use SenRouter\Http\Dispatcher\R; $router = new Router(); /** * call from $router Object **/ $router ->get('hello/{name}', function ($name) { return "Hello $name"; }); /** * call from static method **/ R::('hello.{name}', function ($name) { return "Hello $name"; }) ->separator("."); /** * Call from static method * mixte route params and query params * https://example.com/user/moussa_ndour@hotmail.fr/bicis?order_by=name */ R::get('users/{email}/{entreprise}', function ($email, $entreprise) { $orderBy = Input::one('order_by'); return "Hello $email $entreprise with query $orderBy"; }); /** * Call from static method **/ R::get('calcul.{num1}.{num2}', function ($num1, $num2) { return Response::withJson([ 'result' => $num1 + $num2 ]); }) ->regex([ 'num1' => '\d+', 'num2' => '\d+' ]) ->middleware([ 'HomeMiddleware@isOdd', function ($num1, $num2) { if ($num1 == $num2) { return false; } } ]) ->separator("."); $router->run(); //or R::run();
use SenRouter\Http\Response;
class HomeController{
public function sum($num1, $num2){
return Response::withJson([
'result' => $num1 + $num2
]);
}
}
use SenRouter\Http\Response;
class HomeMiddleware{
/**
* Return strict value False or no empty string to block
* returned value will be send as request response
*/
public function isOdd($num1, $num2){
if($num1 % 2 !== 0 || $num2 % 2 !== 0 )
{
return false;// return 'some_string';
}
}
}