In one of my projects, I have to change the menu links
according to my content page
. Its like, if my "content page" is page 1
, then my menu links will be Menu 1, Menu 2, Menu 3,...
, if my content page is page 2
, my menu links will be Menu A, Menu B, Menu C,...
.
Example:
in my header, menu bar
is :
<ul>
<li><?php echo $this->Html->link("Menu 1",array('controller'=>'controllers','action'=>'menu1','full_base'=>true));?></li>
<li><?php echo $this->Html->link("Menu 2",array('controller'=>'controllers','action'=>'menu2','full_base'=>true));?></li>
</ul>
What I want is, when I am in page "any_page.ctp"(means, when I am in a function "anyPage"), the menu bar will be automatically changed to this :
<ul>
<li><?php echo $this->Html->link("Menu A",array('controller'=>'controllers','action'=>'menuA','full_base'=>true));?></li>
<li><?php echo $this->Html->link("Menu B",array('controller'=>'controllers','action'=>'menuB','full_base'=>true));?></li>
</ul>
I want to change the Menu(s)
according to my pages. Is there any way to do this, in CakePHP
?
Please let me know if any more explanation is needed.
Leaving out all the <li>
's and CSS styling you might want to use, you could try something like this in your navbar.
if ($this->params['action'] == 'page1') {
echo "menu item 1";
echo "menu item 1";
echo "menu item 1";
} elseif ($this->params['action'] == 'page2') {
echo "menu item A";
echo "menu item B";
echo "menu item C";
} else {
echo "something else A";
echo "something else B";
echo "something else C";
}
Basically, if you want to access your current action in a view, you can get it via $this->params['action']
. To see other variables that are available, try using debug($this->params)
.