如何为 CakePHP 控制器中的某些操作创建自定义路由?
来自 Django,我习惯于能够以我认为合适的任何方式组织我的 URL 路由。例如,用户注册将存在于 /users/
url 下。
/users/
/users/registration/
/users/registration/optin/
/users/registration/thankyou/
但是,我正在 CakePHP 中创建一个用户注册系统,并且 url 路由的默认约定行为是 /controller/action/
。这会产生:
/users/
/users/registration/
/users/optin/
/users/thankyou/
如何在我的某些操作上实现 /controller/action/custom/
style-url 路由(其中 /custom/
是一个子部分,而不是一个参数)?我是否应该期待这样做,或者我只是在反对惯例?
Coming from Django, I'm used to be able to organize my URL routings any way I see fit. For example, user registration would exist under the /users/
url.
/users/
/users/registration/
/users/registration/optin/
/users/registration/thankyou/
However, I'm creating a user registration system in CakePHP and the default convention behaviour for url routings is /controller/action/
. This yields:
/users/
/users/registration/
/users/optin/
/users/thankyou/
How can I achieve a /controller/action/custom/
style-url routing on some of my actions (where /custom/
is a sub-section, not a parameter)? Should I even be expecting to do this or am I just fighting the convention?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
/controller/action/custom/
默认情况下工作正常。它调用
ControllerController::action('custom')
。如果您正在寻找类似通过 URL
/users/registration/thankyou
调用UsersController::thankyou()
的内容,您可以创建适当的路由:您可以对路由进行分组在这样的一条规则中:
在 Cake 中,路由非常强大且完全灵活。一如既往,学习手册:
http://book.cakephp.org/view/945/Routes-Configuration
基本原理是路由器将路由(例如
'/users/registration/:action'
)与当前 URL 进行匹配,包括第三个参数中指定的条件,例如'action' = > '(optin|thankyou)'
(:action
部分必须与正则表达式/^(optin|thankyou)$/
匹配)。如果匹配,它会将第二个参数的默认值与从 URL 中提取的任何信息合并,因此您将得到 array('controller' => 'users', 'action' => 'thankyou')array('controller' => 'users', 'action' => 'thankyou') 例如。
然后它通过
CakeRoute::parse
推送它,它构造了您在控制器中执行debug($this->params)
时可以看到的数组。该数组用于确定要加载哪个控制器以及要调用哪个操作。基本的正则表达式和参数匹配已经非常强大,但是通过子类化
CakeRoute
并提供自定义parse
函数,您可能会变得完全疯狂,如手册末尾的简要说明。 :)/controller/action/custom/
works fine by default.It invokes
ControllerController::action('custom')
.If you're looking for something like invoking
UsersController::thankyou()
through the URL/users/registration/thankyou
, you can make an appropriate route:You can group routes in one rule like this:
Routes are very powerful and completely flexible in Cake. As always, study the manual:
http://book.cakephp.org/view/945/Routes-Configuration
The basics are that the Router matches the route, e.g.
'/users/registration/:action'
, against the current URL, including conditions specified in the third parameter, e.g.'action' => '(optin|thankyou)'
(the:action
part has to match the RegEx/^(optin|thankyou)$/
).If it matches, it merges the defaults from the second parameter with any information extracted from the URL, so you get
array('controller' => 'users', 'action' => 'thankyou')
for example.It then pushes it through
CakeRoute::parse
, which constructs the array you can see when doingdebug($this->params)
in a controller. This array is used to figure out which Controller to load and which action to invoke.The basic RegEx and parameter matching is already very powerful, but you can go completely crazy by subclassing
CakeRoute
and providing a customparse
function, as briefly explained at the end of the manual. :)