cakephp 路由修改控制器名称/获取控制器名称

发布于 2024-12-22 18:53:20 字数 553 浏览 0 评论 0原文

我创建了一条与此类似的路由:

Router::connect("/backend/:controller/:action/*");

现在我想路由适合此模式的每个控制器,并将其重命名为类似 backend_:controller 的内容。

类似这样:

Router::connect("/backend/:controller/:action/*", array('controller' => 'backend_:controller'));

示例:如果调用 URL www.example.com/backend/settings/myaction,它将路由到控制器“backend_settings”并调用操作“myaction”!

但另一方面,如果有人调用 www.example.com/settings,它将路由到控制器“设置”。

URL 应该保持它被调用的方式,cakePHP 应该只使用修改后的控制器名称!

我希望有人能指出我应该用于解决此问题的最佳解决方案。提前致谢!

I created a route which is analog to this one:

Router::connect("/backend/:controller/:action/*");

And now I want to route every controller that fits this pattern to be renamed to something like backend_:controller.

Somehow like that:

Router::connect("/backend/:controller/:action/*", array('controller' => 'backend_:controller'));

Example: If the URL www.example.com/backend/settings/myaction is called, it would route to the controller "backend_settings" and invoke the action "myaction"!

But on the other hand, if some called www.example.com/settings, it would route to the controller "settings".

The URL is supposed to stay the way it was called, cakePHP should only use a modified controller name!

I hope someone can point me to the best solution I am supposed to use for this problem. Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扮仙女 2024-12-29 18:53:20

您可以在指定路由时使用 routeClass 属性,并使用自定义路由类。

这是我的 CakeRoute 类的实现,它完全按照您的描述进行操作(将控制器前缀附加到您的控制器):

// ControllerPrefixRoute.php file in app/Routing/Route/

App::uses('CakeRoute', 'Routing/Route');

class ControllerPrefixRoute extends CakeRoute {

    /**
     * Parses a string url into an array. If a controller_prefix key is found it will be appended to the
     * controller parameter
     *
     * @param string $url The url to parse
     * @return mixed false on failure, or an array of request parameters
     */
    public function parse($url) {

        $params = parent::parse($url);

        if (!$params) {
            return false;
        }
        $params['controller'] = $params['controller_prefix'].'_'.$params['controller'];
        return $params;
    }

}

以下是如何使用它:

// inside routes.php file in app/Config/

App::uses('ControllerPrefixRoute', 'Routing/Route');

Router::connect("/:controller_prefix/:controller/:action/*", array(), array('routeClass' => 'ControllerPrefixRoute'));

所以这个 url /backend/settings/ myaction 将调用 BackendSettingsController::myaction

You can use the routeClass property when specifying your route, and use a custom route class.

This is my implementation of the CakeRoute class that does exactly what you described (appends a controller prefix to your controller):

// ControllerPrefixRoute.php file in app/Routing/Route/

App::uses('CakeRoute', 'Routing/Route');

class ControllerPrefixRoute extends CakeRoute {

    /**
     * Parses a string url into an array. If a controller_prefix key is found it will be appended to the
     * controller parameter
     *
     * @param string $url The url to parse
     * @return mixed false on failure, or an array of request parameters
     */
    public function parse($url) {

        $params = parent::parse($url);

        if (!$params) {
            return false;
        }
        $params['controller'] = $params['controller_prefix'].'_'.$params['controller'];
        return $params;
    }

}

And here is how to use it:

// inside routes.php file in app/Config/

App::uses('ControllerPrefixRoute', 'Routing/Route');

Router::connect("/:controller_prefix/:controller/:action/*", array(), array('routeClass' => 'ControllerPrefixRoute'));

So this url /backend/settings/myaction will invoke BackendSettingsController::myaction

毁我热情 2024-12-29 18:53:20

也许您需要的是路由器前缀。

转到 core.php 并添加这一行:

Configure::write('Routing.prefixes', array('backend'));

仅此而已...您不需要添加路由..所以现在 www.example.com/backend/settings/add 将查找设置控制器中名为 backend_add() 的方法

并且 www.example.com/settings/add 将调用名为 add() 的方法设置控制器

在这里你会找到更好的例子=)

希望这有帮助

maybe what you need is a router prefix.

go to the core.php and add this line:

Configure::write('Routing.prefixes', array('backend'));

and that's all... you don't need to add routes.. so now www.example.com/backend/settings/add will look for a method called backend_add() in the Settings controller

And www.example.com/settings/add will call the method called add() in the Settings controller

here you'll find better examples =)

hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文