inhere/sroute 很是轻量级的单一文件的路由器。简洁、自定义性强php
referrer the project noahbuschermacaw , but add some feature.git
支持请求方法: GET
POST
PUT
DELETE
HEAD
OPTIONS
github
支持事件: found
notFound
. 你能够作一些事情当触发事件时(好比记录日志等)json
支持设置匹配路由的解析器: SRoute::setMatchedRouteParser()
. 你能够自定义如何调用匹配的路由处理程序.bash
支持自动匹配路由到控制器就像 yii 同样, 请参看配置项 autoRoute
.服务器
支持手动调度一个路由经过方法 SRoute::dispatchTo()
闭包
你也能够不配置任何东西, 它也能很好的工做app
{ "require": { "inhere/sroute": "dev-master" } }
首先, 导入类框架
use inhere\sroute\SRoute;
// 匹配 GET 请求. 处理器是个闭包 Closure SRoute::get('/', function() { echo 'hello'; }); // 匹配参数 'test/john' SRoute::get('/test/(\w+)', function($arg) { echo $arg; // 'john' }); // 匹配 POST 请求 SRoute::post('/user/login', function() { var_dump($_POST); }); // 匹配 GET 或者 POST SRoute::map(['get', 'post'], '/user/login', function() { var_dump($_GET, $_POST); }); // 容许任何请求方法 SRoute::any('/home', function() { echo 'hello, you request page is /home'; });
若是配置了
'ignoreLastSep' => true
, '/index' 等同于 '/index/'yii
SRoute::get('/index', 'app\controllers\Home@index');
动态匹配控制器方法, 需配置 'dynamicAction' => true
NOTICE: 使用动态匹配控制器方法, 应当使用
any()
添加路由. 即此时没法限定请求方法REQUEST_METHOD
// 访问 '/home/test' 将会执行 'app\controllers\Home::test()' SRoute::any('/home/(\w+)', app\controllers\Home::class); // 可匹配 '/home', '/home/test' 等 SRoute::any('/home(/\w+)?', app\controllers\Home::class);
上面两个的区别是 第一个没法匹配
/home
配置 actionExecutor 为你须要的方法名,例如配置为 'actionExecutor' => 'run'
,那全部的方法请求都会提交给此方法。
会将真实的action名做为参数传入run($action)
, 须要你在此方法中调度来执行真正的请求方法。
在你须要将路由器整合到本身的框架时颇有用
示例:
// 访问 '/user', 将会调用 app\controllers\User::run('') SRoute::get('/user', 'app\controllers\User'); // 访问 '/user/profile', 将会调用 app\controllers\User::run('profile') SRoute::get('/user/profile', 'app\controllers\User'); // 同时配置 'actionExecutor' => 'run' 和 'dynamicAction' => true, // 访问 '/user', will call app\controllers\User::run('') // 访问 '/user/profile', will call app\controllers\User::run('profile') SRoute::get('/user(/\w+)?', 'app\controllers\User');
支持自动匹配路由到控制器就像 yii 同样, 需配置 autoRoute
.
'autoRoute' => [ 'enable' => 1, // 启用 'controllerNamespace' => 'examples\\controllers', // 控制器类所在命名空间 'controllerSuffix' => 'Controller', // 控制器类后缀 ],
配置 'matchAll' 可用于拦截全部请求。 (例如网站维护时)
可容许配置 'matchAll' 的值为
路由path
'matchAll' => '/about', // a route path
将会直接执行此路由。
回调
'matchAll' => function () { echo 'System Maintaining ... ...'; },
将会直接执行此回调
SRoute::any('/404', function() { echo "Sorry,This page {$_GET['path']} not found."; });
// 成功匹配路由 SRoute::on(SRoute::FOUND, function ($uri, $cb) use ($app) { $app->logger->debug("Matched uri path: $uri, setting callback is: " . is_string($cb) ? $cb : get_class($cb)); }); // 当匹配失败, 重定向到 '/404' SRoute::on('notFound', '/404'); // 或者, 当匹配失败, 输出消息... SRoute::on('notFound', function ($uri) { echo "the page $uri not found!"; });
// set config SRoute::config([ 'stopOnMatch' => true, 'ignoreLastSep' => true, 'dynamicAction' => true, // 'matchAll' => '/', // a route path // 'matchAll' => function () { // echo 'System Maintaining ... ...'; // }, // enable autoRoute, work like yii framework // you can access '/demo' '/admin/user/info', Don't need to configure any route 'autoRoute' => [ 'enable' => 1, 'controllerNamespace' => 'examples\\controllers', 'controllerSuffix' => 'Controller', ], ]);
默认配置以下
// 全部的默认的配置 [ // stop on matched. only match one 'stopOnMatch' => true, // Filter the `/favicon.ico` request. 'filterFavicon' => false, // ignore last '/' char. If is True, will clear last '/', so '/home' equals to '/home/' 'ignoreLastSep' => false, // match all request. // 1. If is a valid URI path, will match all request uri to the path. // 2. If is a callable, will match all request then call it 'matchAll' => '', // eg: '/site/maintenance' or `function () { echo 'System Maintaining ... ...'; }` // auto route match @like yii framework 'autoRoute' => [ // If is True, will auto find the handler controller file. 'enable' => false, // The default controllers namespace, is valid when `'enable' = true` 'controllerNamespace' => '', // eg: 'app\\controllers' // controller suffix, is valid when `'enable' = true` 'controllerSuffix' => '', // eg: 'Controller' ], // default action method name 'defaultAction' => 'index', // enable dynamic action. // e.g // if set True; // SRoute::any('/demo/(\w+)', app\controllers\Demo::class); // you access '/demo/test' will call 'app\controllers\Demo::test()' 'dynamicAction' => false, // action executor. will auto call controller's executor method to run all action. // e.g // `run($action)` // SRoute::any('/demo/(:act)', app\controllers\Demo::class); // you access `/demo/test` will call `app\controllers\Demo::run('test')` 'actionExecutor' => '', // 'run' ]
NOTICE: 必须在调用
SRoute::dispatch()
以前使用SRoute::config()
来进行一些配置
SRoute::dispatch();
你能够经过 bash ./php_server
来运行一个测试服务器, 如今你能够访问 http://127.0.0.1:5670