Careful with the links in the layout

When you building application most likely there are common links within the layout. They could be in a menu, or let’s say “edit profile” and ‘logout’ links which are visible everywhere. As you probably know if you use Html Helper the link could be either Cake-relative URL or array of URL parameters.

The problem may occur if you using the second option – the array.

For example if we have link like this:

$this->Html->link('View all comments', array('controller'=>'comments', 'action'=>'index');

which will produce link like:

<a href="/comments/index">View all comments</a>

The problem came if the application uses plugins and the user is in inside the plugin. Then the generated link will be:

<a href="/myplugin/comments/index">View all comments</a>

Which is not the correct one.

So the idea is:
if you adding links in the layout with array of URL parameters, always add an extra parameter ‘plugin’=>false to prevent problems like this.

So the link should look like:

$this->Html->link('View all comments', array('plugin'=>null, 'controller'=>'comments', 'action'=>'index');

and it will produce the correct code. Otherwise you risk to have broken links.

3 thoughts on “Careful with the links in the layout

  1. Pingback: CakePHP : signets remarquables du 23/04/2010 au 26/04/2010 | Cherry on the...

  2. Nick

    Not quite right. Setting the ‘plugin’ to false will yield unexpected results. Instead, use ‘plugin’ => null

    $this->Html->link(‘View all comments’, array(‘plugin’=>null, ‘controller’=>’comments’, ‘action’=>’index’);

Leave a Reply

Your email address will not be published. Required fields are marked *