cakephp 路由修改控制器名称/获取控制器名称
我创建了一条与此类似的路由:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在指定路由时使用
routeClass
属性,并使用自定义路由类。这是我的
CakeRoute
类的实现,它完全按照您的描述进行操作(将控制器前缀附加到您的控制器):以下是如何使用它:
所以这个 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):And here is how to use it:
So this url
/backend/settings/myaction
will invokeBackendSettingsController::myaction
也许您需要的是路由器前缀。
转到 core.php 并添加这一行:
仅此而已...您不需要添加路由..所以现在
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:
and that's all... you don't need to add routes.. so now
www.example.com/backend/settings/add
will look for a method calledbackend_add()
in the Settings controllerAnd
www.example.com/settings/add
will call the method calledadd()
in the Settings controllerhere you'll find better examples =)
hope this helps