This is not a real question, I need a confirmation to know if I understand what I'm studying (the routes of CakePHP).
I have the plugin MyPlugin
. By default, all requests should be directed to the plugin, so I wish that the plugin name doesn't appear in the url.
For example:
/pages
should be resolved as:
['controller' => 'pages', 'action' => 'index', 'plugin' => 'MyPlugin']
The same should apply to the "admin" prefix.
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.
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.
Thanks.
EDIT
Thanks to PGBI, this code should be final:
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.