cakePHP使用两个控制器路由


cakePHP Routing using two controllers

我使用了cakePHP框架。我对cakephp中的路由有问题。

我有两个控制器"Admins"answers"Users"。

我为"Users"控制器中的"Admins"控制器写了一些方法。

class UsersController {
    public function admin_index() {
       my_code
    }
}
class AdminsController {
    My methods
}

根据cakephp路由规则,如果我想使用admin控制器的admin_index方法,请使用下面的URL:

site_url/admin/users/index

我想从上面的URL删除admin关键字。

我在谷歌上搜索了很多,但我没有得到任何合适的解决方案。

提前感谢你的帮助。

如果你需要从AdminsController中使用admin_index,那么你的url应该是site_url/admin/admins/index

同时检查core.php文件中的这一行:

https://github.com/cakephp/cakephp/blob/2.5/app/Config/core.php L152

如果你需要使用site_url/admin/users/index AdminsController内的admin_index方法,那么我宁愿从核心文件禁用路由,并再次写入路由。

CakePHP - Routing Using 'admin_'前缀。可能会有帮助

我找到解决办法了。

路由器连接中的用户前缀

Router::connect('/:controller/:action/*', array('prefix' => 'admin', 'admin' => true, 'controller' => 'admins'));

如果你想在你的参数中添加一个条件,比如(':action'是'addUser':在这个例子中,这个路由器只有当它的动作是'addUser'时才会起作用),那么使用下面的代码

Router::connect('/:controller/:action/*', array('prefix' => 'admin', 'admin' => true, 'controller' => 'admins'),
                                          array('action'=>'index|edit'));