Creating meaningful add URLs in 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) {
// fun开发者_如何学编程ction 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?
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)
.
精彩评论