1

I have a site developed in cakephp 2 and I want that into my default.ctp there is a menu with some elements taked from the database. How can I change the elements which controller I have to use? Where I have to put my query? Because if I was into a model is easy but into the default.ctp? How can I do that? Here is my default.ctp:

<body> <div class="content"> <div id="navigation"> <ul> <?php if (!empty($authUser)) { ?> <li><?php echo $this->Html->link('I want to change this', array('controller' => 'ingredients', 'action' => 'index')); ?></li> <li><?php echo $this->Html->link('I want to change this', array('controller' => 'brands', 'action' => 'index')); ?></li> <li><?php echo $this->Html->link('I want to change this', array('controller' => 'manufacturers', 'action' => 'index')); ?></li> <?php // $is_logged from UsersController->beforeFilter echo $this->element ('header_menu_logged'); } else { ?> <li><?php echo $this->Html->link('Competizioni', array('controller' => 'brands', 'action' => 'index')); ?></li> <li><?php echo $this->Html->link('Cerca', array('controller' => 'manufacturers', 'action' => 'index')); ?></li> <?php // $current_model echo $this->element ('header_menu', array('selected' => 'Pippo')); } ?> </ul> </div> <div class="page"> <?php // messaggi di stato per le azioni if (empty($flash_element)) { $flash_element = $this->Session->read('flash_element'); if (empty($flash_element)) { $flash_element = 'default'; } } // echo '>'.$flash_element.'<'; $auth_msg = $this->Session->flash('auth', array ('element' => 'flash_'.$flash_element)); $flash_msg = $this->Session->flash('flash', array ('element' => 'flash_'.$flash_element)); if (!empty($auth_msg)) { echo $auth_msg; } if (!empty($flash_msg)) { echo $flash_msg; } ?> <section><!--class="contents"--> <?php echo $content_for_layout; ?> </section> </div> </div> </body> 

1 Answer 1

5

Well, I suggest doing $this->set('anyString', $varStringOfTheLink); in your beforeRender() or beforeFilter() in appController.php .

Do your query from there and then use the set() function to set a variable for your view.

Then in default.ctp you will be able to use $anyString.

So to do a quick wrap-up. Do the query in your appController, before filter or before render, then set it so your view can use it. The first parameter of the set() function is the name of the var you want to use in your view. Just put a $ before. The second paremeter is the value of the variable you want.

//appController.php function beforeFilter(){ $myQueryVar = $this->Model->find('whateverIWant'); $this->set('myLinkOne', $myQueryVar); } //layouts/default.ctp echo $this->Html->link($myLinkOne, array('controller' => 'ingredients', 'action' => 'index')); 
Sign up to request clarification or add additional context in comments.

Comments