插件开发手册

路由

最近更新:6个月前

路由开源地址:https://github.com/ebcms/router

默认路由

上面讲的是自定义路由的情况,若是没有定义路由,系统会有默认路由,规则如下:

/包/路径

举例:

  • /plugin/demo/index 路由到 App\Plugin\Demo\Http\Index
  • /plugin/demo/admin/create 路由到 App\Plugin\Demo\Http\Admin\Create
  • /plugin/demo/admin/create-category 路由到 App\Plugin\Demo\Http\Admin\CreateCategory
  • /plugin/demo/show-item 路由到 App\Plugin\Demo\Http\ShowItem

路由规则

插件的路由规则是:网站/index.php/plugin/plugin_name/path..

示例1

  • 地址:网站/index.php/plugin/demo/index
  • 指向:plugin/demo/src/library/Http/Index.php

示例2

  • 地址:网站/index.php/plugin/demo/admin/login
  • 指向:plugin/demo/src/library/Http/Admin/Login.php

示例3

  • 地址:网站/index.php/plugin/demo/admin/log-out
  • 指向:plugin/demo/src/library/Http/Admin/LogOut.php

地址生成器

$router->buildUrl('路由地址')

示例1

  • 路由地址:$router->buildUrl('/plugin/demo/index')
  • 生成结果:http://..../index.php/plugin/demo/index

示例2

  • 路由地址:$router->buildUrl('/plugin/demo/admin/log-out')
  • 生成结果:http://..../index.php/plugin/demo/admin/log-out

路由美化

可以通过自定义路由实现美化url

示例:

$router->getCollector()->addGroup('http://www.demo.com', function (Ebcms\Router\Collector $collector) {
    $collector->get('/', \App\Plugin\Demo\Http\Index::class);
    $collector->get('/admin_login', \App\Plugin\Demo\Http\Admin\Login::class);
    $collector->get('/admin_logout', \App\Plugin\Demo\Http\Admin\LogOut::class);
});

注意:路由注册必须在路由解析之前完成,通常挂载到app.init上!!也就是在目录/src/hook/app.init/下面建一个文件放入上面的代码

通过上面的路由代码后,生成器生成的路由将会发生变化:

示例1

  • 路由地址:$router->buildUrl('/plugin/demo/index')
  • 生成结果:http://www.demo.com/index.php/

示例2

  • 路由地址:$router->buildUrl('/plugin/demo/admin/login')
  • 生成结果:http://www.demo.com/index.php/admin_login

示例3

  • 路由地址:$router->buildUrl('/plugin/demo/admin/log-out')
  • 生成结果:http://www.demo.com/index.php/admin_logout

路由分组

另外也支持路由分组,例如:

$router->getCollector()->addGroup('/group', function($collector){
    $collector->get('/path1', ControlerA::class);
    $collector->get('/path2', ControllerB::class);
    $collector->addGroup('/subgroup', function($collector){
        $collector->get('/subpath', ControllerC::class);
    });
});

支持多级嵌套

绑定中间件

// 单个绑定
$router->getCollector()->get('/index', Index::class, '', ['middleware1', 'middleware2']);

// 绑定到分组(该分组下所有的路由都会绑定)
$router->getCollector()->addGroup('/group', function($collector){
    $collector->bindMiddlewares(['somemiddleware1', 'somemiddleware2']);
    $collector->get...
});

绑定参数

// 单个绑定
$router->getCollector()->get('/index', Index::class, '', [], ['somekey'=>'someval']);

// 绑定到分组(该分组下所有的路由都会绑定)
$router->getCollector()->addGroup('/group', function($collector){
    $collector->bindParams(['somekey'=>'someval']);
    $collector->get...
});

路由命名

$router->getCollector()->get('/foo', Index::class, '/name1');
$router->getCollector()->get('/bar/{id:\d+}', Index::class, '/name2');
$router->getCollector()->addGroup('/group', function($collector){
    $collector->get('/sub[/{id:\d+}]', Index::class, '/name3');
});

绑定域名

示例代码如下:

$router->getCollector()->addGroup('http://www.ebcms.com/sub', function($collector){
    $collector->get('/foo', ControlerA::class,'/name-1');
    $collector->addGroup('/sub2', function($collector){
        $collector->get('/cc', ControllerC::class, '/name-2');
        $collector->get('/cc/ddd.html', ControllerC::class, '/name-houzui');
    });
});

$router->buildUrl('/name-1'); // http://www.ebcms.com/sub/foo

$router->buildUrl('/name-2'); // http://www.ebcms.com/sub/sub2/cc

$router->buildUrl('/name-houzui'); // http://www.ebcms.com/sub/sub2/cc/ddd.html

通常,要是不绑定域名,不限制https或http的话 http://www.ebcms.com/ 可以通过代码获取当前的域名而不必固定写死

去掉index.php

配置伪静态可去掉index.php

nginx

location / {  
    try_files $uri $uri/ /index.php$is_args$query_string;  
}  

apache

<IfModule mod_rewrite.c>
  Options +FollowSymlinks -Multiviews
  RewriteEngine On

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php [QSA,PT,L,E=PATH_INFO:$1]
</IfModule>

Powered By EBCMS