如何为 CakePHP 控制器中的某些操作创建自定义路由?

发布于 2024-09-11 21:03:20 字数 513 浏览 8 评论 0原文

来自 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 技术交流群。

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

发布评论

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

评论(1

蓝眼泪 2024-09-18 21:03:20

/controller/action/custom/ 默认情况下工作正常。
它调用ControllerController::action('custom')

如果您正在寻找类似通过 URL /users/registration/thankyou 调用 UsersController::thankyou() 的内容,您可以创建适当的路由:

Router::connect('/users/registration/thankyou',
                array('controller' => 'users', 'action' => 'thankyou'));

您可以对路由进行分组在这样的一条规则中:

// Routes /users/registration/optin and /users/registration/thankyou to
// UsersController::optin() and UsersController::thankyou() respectively
Router::connect('/users/registration/:action',
                array('controller' => 'users'),
                array('action' => '(optin|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:

Router::connect('/users/registration/thankyou',
                array('controller' => 'users', 'action' => 'thankyou'));

You can group routes in one rule like this:

// Routes /users/registration/optin and /users/registration/thankyou to
// UsersController::optin() and UsersController::thankyou() respectively
Router::connect('/users/registration/:action',
                array('controller' => 'users'),
                array('action' => '(optin|thankyou)'));

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 doing debug($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 custom parse function, as briefly explained at the end of the manual. :)

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