整合CakePHP路由 [英] Consolidate CakePHP routes

查看:120
本文介绍了整合CakePHP路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将以下规则合并为一个规则?

Is there any way to consolidate the following rules into one rule?

Router::connect('/manufacturer/:manufacturer/:friendly0', array('controller'=>'categories', 'action'=>'view'), array('pass'=>array('manufacturer', 'friendly0')));
Router::connect('/manufacturer/:manufacturer/:friendly0/:friendly1', array('controller'=>'categories', 'action'=>'view'), array('pass'=>array('manufacturer', 'friendly0', 'friendly1')));
Router::connect('/manufacturer/:manufacturer/:friendly0/:friendly1/:friendly2', array('controller'=>'categories', 'action'=>'view'), array('pass'=>array('manufacturer', 'friendly0', 'friendly1', 'friendly2')));
Router::connect('/manufacturer/:manufacturer/:friendly0/:friendly1/:friendly2/:friendly3', array('controller'=>'categories', 'action'=>'view'), array('pass'=>array('manufacturer', 'friendly0', 'friendly1', 'friendly2', 'friendly3')));
Router::connect('/manufacturer/:manufacturer/:friendly0/:friendly1/:friendly2/:friendly3/:friendly4', array('controller'=>'categories', 'action'=>'view'), array('pass'=>array('manufacturer', 'friendly0', 'friendly1', 'friendly2', 'friendly3', 'friendly4')));


推荐答案

实际上 - 你不需要使用那么多规则:

Actually - you don't need to use that many rules:

http:// book.cakephp.org/view/945/Routes-Configuration

本质上,当有人浏览您的site.com/ manufacturer时,制造商控制器将被调用,并且由于操作未定义 - 它将默认为索引。所以你可以做的只是:

Essentially when someone will browse to yoursite.com/manufacturer - the manufacturer controller will be called, and since an action isn't defined - it will default to index. So what you could do is just:

Router::connect('/manufacturer/*', array('controller' => 'categories', 'action' => 'view'));

现在,当有人浏览yoursite.com/manufacturer时,请求会转发到类别控制器视图动作。

Now when someone browses to yoursite.com/manufacturer - the request is forwarded to the categories controller, calling the view action. The '/*' insures any further parameters are also forwarded there.

因此,当有人访问yoursite.com/manufacturer/iamfriendly/iamfriendlytoo时,您可以获得这些参数传递参数/变量

So when someone were to visit yoursite.com/manufacturer/iamfriendly/iamfriendlytoo - you can then get those passed paramaters / variables through

$this->params['pass']

或:

$this->passedArgs

给你以下数组:

Array
(
    [0] => iamfriendly
    [1] => iamfriendlytoo
)

您可以通过使用命名参数进一步增强这一点,因此您会收到以下内容:

You can further enhance this by using named parameters, so you receive something like:

Array
(
    ['manufacturer'] => iamfriendly
    ['friendly0'] => iamfriendlytoo
)

这篇关于整合CakePHP路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆