Question

i am using a template for which there are two different navigation menu.

  1. Main navigation menu. ( fixed )

  2. Action navigation menu, contains elements such as back, save, delete etc. this navigation elements changes according to the controllers being called. while it may exist for some controller and for some it doesn't

i am making use of layout, and i have placed all the template code in my default.phtml layout file. the problem i face is for action navigation menu. since the html code for this menu resides in default.phtml i need to change the content of it according to the controller being called.

i am not sure if this is the right way of doing it. but in my default.phtml i am checking the controller name and accordingly displaying the menu. this is the code i am using.

<?php if(Zend_Controller_Front::getInstance()->getRequest()->getControllerName() == 'item'): ?>
    <!-- Action Navigation Menu -->
    <div class="statsRow">
        <div class="wrapper">
            <a href="#"><img src="/images/icons/dark/add.png" alt="" class="icon"><span>New item</span></a>
            <a href="#"><img src="/images/icons/dark/cd.png" alt="" class="icon"><span>Publish / Unpublish item</span></a>
            <a href="#"><img src="/images/icons/dark/trash.png" alt="" class="icon"><span>Delete item</span></a>
        </div>
    </div>
    <div class="line"></div>
<?php endif; ?>

is it okay to do it like this? or do i have any better way of doing this?

Was it helpful?

Solution

It seems that your Action navigation menu acts actually like a SubMenu, where each Action is part of a subpage. A good way to do such a thing is to have two instances of Zend_Navigation, that you could store in the registry for example. Then, in your controllers, you could call an Action Helper in the preDispatch() method like this:

// in each controller where you want your "action navigation menu"
public function preDispatch()
{
    $this->_helper->navigation()->renderActionNavigation();
}

Of course, only controllers that need this Action navigation menu would have this method. This Action Helper will basically get the current view object, create a placeholder and render a partial as follows:

// in your library/My/Controller/Action/Helper
class My_Controller_Action_Helper_Navigation extends Zend_Controller_Action_Helper_Abstract
{
    private $_view = null;

    public function direct()
    {
        $this->_view = $view = Zend_Layout::getMvcInstance()->getView();
        $this->_view->placeholder('action-navigation');
        return $this;
    }

    public function renderActionNavigation()
    {
        $this->_view->render('partials/_action-navigation.phtml');
    }
}

In case you don't use a library, simply put this code in /views/helpers/ and rename the class Zend_View_Helper_Navigation.

Then the partial will be in charge to render your subMenu using a placeholder:

// in /view/scripts/partials/_action-navigation.phtml
<?php $this->placeholder('action-navigation')->captureStart() ?>
<?php $options = array('onlyActiveBranch' => true); ?>
<?= $this->navigation()->menu()->renderMenu(Zend_Registry::get('nav.action-navigation'), $options);
// here I assume that you've stored your Navigation container in the registry ?>
<?php $this->placeholder('action-navigation')->captureEnd() ?>

Additionally, rendering your menu using the navigation view helper method renderMenu() with an option onlyActiveBranch set to true will allow your to only render the active branch, where each branch would correspond to your controllers.

And finally, in your layout you would have this:

 // in your layout file (usually named layout.phtml)
<?= $this->placeholder('action-navigation'); ?>

If you choose to register your Zend_Navigation container in the registry, this could be done in the bootstrap this way:

// in your bootstrap.php
protected function _initNavigation()
{
    $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'nav');
    $container = new Zend_Navigation($config);
    Zend_Registry::set('nav.action-navigation', $container);
}

For more information about containers, refer to this page. Also, if you're not familiar with placeholders, here is a good practical example about how to use them.

OTHER TIPS

I guess it's ok. I'd instead do it in the controller and include the navigation using Zend_Navigation, then you can make use of the ACL etc.

If the controller just set:

$this->view->navigation()->setContainer($navigation);

Where $navigation is an instance of Zend_Navigation. Then in your layout:

<?=$this->navigation()->menu()?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top