Reputation: 248
I have the following route:
$routes->resources('Articles', function ($routes) {
$routes->resources('Comments');
});
I want to link to all comments of the article with id 4:
articles/4/comments
How can I create a link to this url with cakes HtmlHelper?
More about nested routes: https://book.cakephp.org/3.0/en/development/routing.html#creating-nested-resource-routes
Upvotes: 0
Views: 202
Reputation: 60503
Look at the example route patterns given in the linked docs, the nested Articles
> Comments
resources will create routes for Comments
with the following patterns:
/articles/:article_id/comments
/articles/:article_id/comments/:id
You can also check $ bin/cake routes
to get a list of all connected routes with their patterns and defaults. The route you're looking for will be listed there as something like this:
+----------------+--------------------------------+--------------------------------------------------------------------------+
| Route name | URI template | Defaults |
+----------------+--------------------------------+--------------------------------------------------------------------------+
| comments:index | /articles/:article_id/comments | {"controller":"Comments","action":"index","_method":"GET","plugin":null} |
All resource routes are bound to specific HTTP methods (as can be seen above in the defaults column), ie internally the _method
option is used, and the parent ID is prefixed with the singular controller/resource name.
To match the Comments
index, simply target the Comments
controller and index
action as usual. Additionally pass the corresponding _method
(for index
that's GET
), and pass the parent ID in a named fashion, ie as article_id
, like:
[
'controller' => 'Comments',
'action' => 'index',
'_method' => 'GET',
'article_id' => 4
]
See also
Upvotes: 2
Reputation: 1135
You could join Html and Url helpers, like this:
<?=
$this->Html->link(
'Enter',
$this->Url->build('/articles/4/comments', true),
['class' => 'button', 'target' => '_blank']
);
?>
See also:
Upvotes: 0