在 Cakephp 中创建有意义的添加 URL

发布于 2024-09-02 09:40:18 字数 405 浏览 1 评论 0原文

对于我的网站,我有许多订单,每个订单都包含许多报价。报价始终与单个订单相关联,因此在报价控制器中,我添加了一个引用其订单的报价:

function add($orderId) {
    // function here
}

调用 URL 看起来有点像

http://www.example.com/quotes/add/1

我想到,如果 URL 看起来更像是更有意义

http://www.example.com/orders/1/quotes/add

由于报价被添加到订单 1 中。

这可以在 CakePHP 中实现吗?

For my site I have a number of Orders each of which contains a number of Quotes. A quote is always tied to an individual order, so in the quotes controller I add a quote with reference to it's order:

function add($orderId) {
    // function here
}

And the calling URL looks a bit like

http://www.example.com/quotes/add/1

It occurred to me the URLs would make more sense if they looked a bit more like

http://www.example.com/orders/1/quotes/add

As the quote is being added to order 1.

Is this something it's possible to achieve in CakePHP?

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

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

发布评论

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

评论(1

遥远的绿洲 2024-09-09 09:40:19

查看定义路由的文档。

像这样的事情应该可以解决问题:

Router::connect(
    '/orders/:id/quotes/add',
    array('controller' => 'quotes', 'action' => 'add'),
    array('id' => '[0-9]+')
);

您将能够使用 QuotesController::add() 中的 $this->params['id'] 访问 ID。

编辑:

另外,请查看 将参数传递给操作

可以将 ID 作为控制器操作的参数传递,如下所示:

Router::connect(
    '/orders/:id/quotes/add',
    array('controller' => 'quotes', 'action' => 'add'),
    array('pass' => array('id'), 'id' => '[0-9]+')
);

然后您可以在 QuotesController::add($id) 中使用 $id 访问 ID 。

Have a look at the documentation for defining routes.

Something like this should do the trick:

Router::connect(
    '/orders/:id/quotes/add',
    array('controller' => 'quotes', 'action' => 'add'),
    array('id' => '[0-9]+')
);

You will be able to access the ID with $this->params['id'] in QuotesController::add().

Edit:

Also, have a look at the documentation for passing parameters to action.

It is possible to pass the ID in as a parameter of the controller action like so:

Router::connect(
    '/orders/:id/quotes/add',
    array('controller' => 'quotes', 'action' => 'add'),
    array('pass' => array('id'), 'id' => '[0-9]+')
);

You can then access the ID with $id in QuotesController::add($id).

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