1 Star 0 Fork 21

果冻编程 / php-srouter

forked from inhere / php-srouter 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

php simple router

非常轻量级的单一文件的路由器。自定义性强

referrer the project noahbuscher\macaw , but add some feature.

  • 支持请求方法: GET POST PUT DELETE HEAD OPTIONS
  • 支持事件: found notFound. 你可以做一些事情当触发事件时(比如记录日志等)
  • 支持设置匹配路由的解析器: SRoute::setMatchedRouteParser(). 你可以自定义如何调用匹配的路由处理程序.
  • 支持自动匹配路由到控制器就像 yii 一样, 请参看配置项 autoRoute.
  • 支持手动调度一个路由通过方法 SRoute::dispatchTo()
  • 你也可以不配置任何东西, 它也能很好的工作

安装

{
    "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/'

使用控制器方法

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 ... ...';
    },

将会直接执行此回调

设置事件处理(if you need)

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!";
});

设置配置(if you need)

// 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();

examples

please the examples folder's codes.

你可以通过 bash ./php_server 来运行一个测试服务器, 现在你可以访问 http://127.0.0.1:5670

License

MIT

The MIT License (MIT) Copyright (c) 2015 Noah Buscher Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

非常轻量级的单一文件的路由器。支持请求方法: GET POST PUT DELETE HEAD OPTIONS. 支持事件: found notFound. 你可以做一些事情当触发事件时(比如记录日志等),支持自动匹配路由到控制器就像 yii 一样 ... ... 展开 收起
PHP
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/qsw/php-srouter.git
git@gitee.com:qsw/php-srouter.git
qsw
php-srouter
php-srouter
master

搜索帮助