Torisuta
Torisuta

Reputation: 431

CakePHP - Submenu in a different element

I have a question about building a submenu. In my layout, not in a view, I got two elements. The first element is the main menu and the second element is the submenu. Its not that kind of a submenu when you hoover over a button from the main menu a magical submenu appears under the main menu.

I understand how to build a dynamic main menu in CakePHP. Model-Controller->View->Element->Layout. But when Im using this same approuch for my submenu, my submenu will be static. Because when Im pressing on a button the content is changing (view) and not the layout.

Which solutions are there to change the submenu to the corresponding pressed button on the main menu?

Controller/MenuController.php

class MenusController extends AppController{

var $name = 'Menus';

function main() {
    if (isset($this->params['requested']) && $this->params['requested'] == true) {

        $menus = $this->Menu->find('all', array( 'conditions' => array('Menu.parent_id' => '0'), array ('order' => 'position')));

        return $menus;
    } else {
        $this->set('menus', $this->Menu->find('all', array( 'conditions' => array('Menu.parent_id' => '0'), array ('order' => 'position'))));
    }
}

function sub() {
    if (isset($this->params['requested']) && $this->params['requested'] == true) {

        $subs = $this->Menu->find('all', array( 'conditions' => array('Menu.parent_id' => '1'), array ('order' => 'position')));

        return $subs;
   // } else {
     //   $this->set('subs', $this->Menu->find('all', array( 'conditions' => array('Menu.controller' => $this->params['controller']), array ('order' => 'position'))));
    }


    //debug($this->params['controller']);
}  
}

Model/Menu.php

class Menu extends AppModel {    
    var $name = 'Menu';    
}

View/Layouts/Default.ctp

<div id="submenu">                    
     <?php echo $this->element('main'); ?>                   
     <?php echo $this->element('sub'); ?>                    
</div> 

Edited

I tried to build a query for the submenu. The plain string does work. When I request the controller name and use it in the query it doesnt work. With debug($this->request['controller']) I get and see the controller name. But why doesnt this work in a query?

 function sub() {
 // $subrequest = "Projects";
 $subrequest = $this->request['controller'];

 if (isset($this->params['requested']) && $this->params['requested'] == true) {
    $subs = $this->Menu->find('all', array( 'conditions' => array('Menu.controller' => $subrequest)));
    return $subs;
 } else {
     $this->set('subs', $this->Menu->find('all', array( 'conditions' => array('Menu.controller' => $subrequest))));
 }
}

Upvotes: 1

Views: 785

Answers (2)

Torisuta
Torisuta

Reputation: 431

Thank you for your time.

The answer in my topic and this topic gave me the final solution.

So What did I do?

  1. I removed the menuController
  2. I made a beforeFilter in the appController and pass the vars to the the elements main and sub.

Upvotes: 0

kcsoft
kcsoft

Reputation: 2947

The entire layout and view is rendered on each page load. You don't need your MenuController. You will need to set the $menus and $subs variables in AppController->beforeFilter method where you can detect what controller you are in and what action. Make sure every controller extends AppController and calls its parent::beforeFiler.

Upvotes: 0

Related Questions