3

What I'm trying to do is to put two dynamic navigation menus in my CakePHP layout (default.ctp). The main menu should have multiple levels (with a dropdown functionality). The secondary menu is the one that shows the dropdown content of the main menu in a left sidebar.

I've read the CakePHP documentation but I'm confused how to fit those menus in the layout. I know that you have 4 different parts in a view layer (as documented in http://book.cakephp.org/2.0/en/views.html):

  • views
  • elements
  • layouts
  • helpers

But with the knowledge I have right now, I think none of this parts can be used to fill my needs. A navigation menu is a part that you only load ONES in a layout, so it isn't an element or a helper. So what's the best practice...

  • ... where to create the menu tree?
  • ... where / how to echo it in the layout file?

Can anybody clearify my issue? Thanks in advance! ;)

Sam
  • 472
  • 1
  • 8
  • 19

1 Answers1

2

You can create your menu tree in element folder for example ...

element/top_menu.ctp

element/side_menu.ctp

now you can include these menu in layout as your requirement at dyanamic condition

for example #

if(#user is admin)
{
   echo $this->Element('top_menu');
}
else if(# user is registered)
{
   echo $this->Element('side_menu');
}
else
{
     echo $this->Element('top_menu');
    echo $this->Element('side_menu');
}

Here put your condition ..and you can use menus as your requirements from Elemnts folder......

  • 1
    Okay, that's clear! But how can I dynamically build the menu in top_menu.ctp? I want to get my data out of the database, so I suppose I shouldn't connect with my database in that .ctp file? If I do so, the proper MVC way of working is totally messed up... – Sam Feb 26 '14 at 19:09
  • 2
    Retrieve your menu values from database in Controller's action method & set value $this->set('menu',$menu_names); and show these values in top_menu.ctp by retrieving values using foreach loop .. – Kuldeep Choudhary Feb 28 '14 at 05:47