문제

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).

다른 팁

you can create elements for menu. for example you want to create the menu for page 1.

<ul>
      <li><?php echo $this->Html->link("Menu",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>

Save this into a file in the path -> app/View/Elements/page1_menu.ctp (filename is just an example)

Then later in view where you display your menu. (For example your url is /users/page1)

if($this->params["controller"]=="users" && $this->params["action"]=="page1" ){
  echo $this->element('page1_menu');
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top