Ibrahim Azhar Armar
Ibrahim Azhar Armar

Reputation: 25745

Action based navigation in Zend Framework


i have navigation menu which is displayed to users based on certain action of the controller. here is what i am doing.

//in each controller-action where i want "action navigation menu"
public function indexAction()
{
    $this->_helper->navigation()->renderActionNavigation();
}
public function newAction()
{
    $this->_helper->navigation()->renderActionNavigation();
}

the navigation menu is displayed accordingly. here is my navigation.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<configdata>
    <item-index>
        <new-item>
            <label>New Item</label>
            <route>admin-item-new</route>
        </new-item>
        <delete-item>
            <label>Delete Item</label>
            <uri>#</uri>
        </delete-item>
    </item-index>
    <item-new>
        <publish-unpublish-item>
            <label>Save &amp; Close</label>
            <uri>#</uri>
        </publish-unpublish-item>
        <delete-item>
            <label>Save &amp; New</label>
            <uri>#</uri>
        </delete-item>
    </item-new>
</configdata>

the parent element of each navigation menu represents a naming convention in the above navigation.xml file for example

`<item-index>` represents item{controller}index{action}
`<item-new>` represents item{controller}new{action}
//and so on

here is the action helper. Navigation.php i am using

class Zend_Controller_Action_Helper_Navigation extends Zend_Controller_Action_Helper_Abstract
{
    private $_view = null;

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

    public function renderActionNavigation()
    {   
        $config = new Zend_Config_Xml(
            APPLICATION_PATH.'/configs/navigation.xml', strtolower(
                $this->getRequest()->getControllerName().'-'.
                $this->getRequest()->getActionName()
            )
        );
        $container = new Zend_Navigation($config);
        $this->_view->partial('partials/_action-navigation.phtml', array('container' => $container));
    }
}

and finally _action-navigation.phtml

<?php $this->placeholder('action-navigation')->captureStart(); ?>
<div class="statsRow">
    <div class="wrapper" >
        <?php foreach($this->container as $page): ?>
            <a href="<?php echo $page->getHref(); ?>" class="<?php echo $page->class; ?>" id="<?php echo $page->id; ?>"></a>
        <?php endforeach; ?>
     </div>
</div>
<?php $this->placeholder('action-navigation')->captureEnd(); ?>

My directory structure is as follows

/application
  /layouts
    admin.phtml
    default.phtml    
  /modules
    /admin
      /controllers
        /helpers
          /Navigation.php
        IndexController.php
      /views
        /helpers
        /scripts
          /partials
            _action-navigation.pthml
            sidebar.phtml
          /index
          /item

the weird behavior i am experiencing is. in my Bootstrap.php file there is an empty _initView() method. my application works properly if this method exist. note that this method is empty. but when i remove it it gives me following error.

Application error

Exception information:

Message: script 'partials/_action-navigation.phtml' not found in path (./views/scripts/)

i am not able to understand this behavior by Zend Framework. how is action-navigation code related to _initView method in Bootstrap? what is happening and any fix for this or any suggestion for improvement of my code?

Update: The problem lies with this line of code

$this->_view->partial('partials/_action-navigation.phtml', array('container' => $container));

Upvotes: 1

Views: 923

Answers (2)

Vitor Almeida
Vitor Almeida

Reputation: 81

I Suggest you use this clean and lightweight way, removing your dependence to inform actual module and mantaining your view script clean without captureStart and End (it's used ob_start... when u do captureStart-end)

$navigation = $this->view->navigation()->menu()->renderPartial($container, 'partials/_action-navigation.phtml');
// U can use placeholders `append` and `prepend` methods too, if u need more control in your placeholder content
$this->view->placeholder('action-navigation')->set($navigation);

Upvotes: 0

Liyali
Liyali

Reputation: 5693

You forgot to add the name of the admin module as second argument:

$this->_view->partial('partials/_action-navigation.phtml', 'admin', array('container' => $container));

Upvotes: 1

Related Questions