CakePHP 3

时间:2017-02-02 03:31:57

标签: cakephp url-routing cakephp-3.3 cakephp-routing

我正在开发CakePHP 3.3

我有一些名为

的仪表板控制器
DashboardUsersController.php, 
DashboardBusinessesController.php,
DashboardCustomersController.php, 
etc

我想要像

那样映射网址
http://example.com/dashboard/users/view/param1/param2

这将调用DashboardUsersController.php类和view函数,param1param2作为参数。

简而言之,我想将网址http://example.com/dashboard-users/view/param更改为http://example.com/dashboard/users/view/param

这种类型的映射只有在域后存在仪表板时才会完成,否则它将像默认情况一样工作,如访问http://example.com/users/view/param1时会调用UsersController.php

到目前为止我做了什么?

因为,我是CakePHP和路由的新手,我不知道从哪里开始,因此到目前为止什么也没做。我需要你的帮助。

1 个答案:

答案 0 :(得分:4)

我认为你需要的是前缀。使用前缀仪表板烘焙控制器模型。

routes.php

中使用此功能
use Cake\Routing\Route\DashedRoute;

Router::prefix('dashboard', function ($routes) {
    // All routes here will be prefixed with `/dashboard
    $routes->fallbacks(DashedRoute::class);
});

从控制器中删除该仪表板部件或从表名中删除仪表板,并使用--prefix重新绑定所有内容。

  

bin / cake bake all --prefix dashboard

这些链接可以帮助您

https://book.cakephp.org/3.0/en/development/routing.html#prefix-routing

https://book.cakephp.org/3.0/en/bake/usage.html