简体   繁体   English

CakePHP 3.x:插件的所有路由

[英]CakePHP 3.x: all routes to the plugin

This is not a real question, I need a confirmation to know if I understand what I'm studying (the routes of CakePHP). 这不是一个真正的问题,我需要确认我是否理解我正在学习的东西(CakePHP的路线)。

I have the plugin MyPlugin . 我有插件MyPlugin By default, all requests should be directed to the plugin, so I wish that the plugin name doesn't appear in the url. 默认情况下,所有请求都应该定向到插件,所以我希望插件名称不会出现在url中。

For example: 例如:

/pages

should be resolved as: 应解决为:

['controller' => 'pages', 'action' => 'index', 'plugin' => 'MyPlugin']

The same should apply to the "admin" prefix. 同样适用于“admin”前缀。

For example: 例如:

/admin/pages

should be resolved as: 应解决为:

['controller' => 'pages', 'action' => 'index', 'plugin' => 'MyPlugin', 'prefix' => 'admin']

In short, you have to imagine as if the application (so except for MyPlugin ) has no controller. 简而言之,您必须想象应用程序(除了MyPlugin之外)没有控制器。

I studied routes (particularly this and this ) and now I would like to know if this code is correct: 我研究了路线 (特别是这个这个 ),现在我想知道这段代码是否正确:

Router::defaultRouteClass('InflectedRoute');

Router::prefix('admin', function ($routes) {
    $routes->plugin('MeCms', ['path' => '/'], function ($routes) {
        $routes->fallbacks();
    });
});

Router::scope('/', ['plugin' => 'MeCms'], function ($routes) {  
    $routes->fallbacks();
});

From my tests, this seems to work. 从我的测试来看,这似乎有效。 But since the routes have changed a lot compared to CakePHP 2.x, I would like to have confirmation that you have understood. 但是,由于与CakePHP 2.x相比,路线发生了很大变化,我想确认您已经理解了。

Thanks. 谢谢。


EDIT 编辑

Thanks to PGBI, this code should be final: 感谢PGBI,这段代码应该是最终的:

Router::scope('/', ['plugin' => 'MeCms'], function ($routes) {
    Router::connect('/admin', ['controller' => 'Pages', 'action' => 'index', 'plugin' => 'MeCms', 'prefix' => 'admin']);

    $routes->prefix('admin', function ($routes) {
        $routes->fallbacks();
    });
    $routes->fallbacks();
});

Yes that's correct. 对,那是正确的。 I think you could do shorter (to be tested, but you get the idea): 我认为你可以做得更短(进行测试,但你明白了):

Router::scope('/', ['plugin' => 'MeCms'], function ($routes) {  
    $routes->prefix('admin', function ($routes) {
        $routes->fallbacks();
    });
    $routes->fallbacks();
});

EDIT: To add a homepage to your admin section : 编辑:要向您的管理部分添加主页:

Router::scope('/', ['plugin' => 'MeCms'], function ($routes) {  
    $routes->prefix('admin', function ($routes) {
        $routes->connect('/', ['controller' => 'Pages', 'action' => 'index']);
        $routes->fallbacks();
    });
    $routes->fallbacks();
});

You don't need to repeat ['plugin' => 'MeCms'] or ["prefix" => "admin"] since it's already defined before. 您不需要重复['plugin' => 'MeCms']["prefix" => "admin"]因为它之前已经定义过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM