Search code examples
phpzend-frameworkzend-framework-mvc

Create a Zend_Navigation_Page_Mvc instance from a Zend_Controller_Request


I have pages in my application that make up the navigation tree. I would like to dynamically insert pages into my navigation using the values of the request. I already have the logic to find the page and then call the addPage() method on it. What I'm looking for is how to easily pass the Zend_Controller_Request values to Zend_Navigation_Page::factory() so I can add that page. Maybe even written as a plugin?


Solution

AngelP got the closest, so I'm giving him credit, but here's my solution:

$request = $this->getRequest();
if ($page = $this->view->siteNav->findBy('id', $page_id)) {
    $page->addPage(Zend_Navigation_Page::factory($request->getParams())
            ->setParams($request->getParams())
            ->setLabel($this->view->title)
            ->setVisible(false));
}

This code is executed from a controller action. $this->view->siteNav is an instance of Zend_Navigation that I have in the view. getParams() from the Zend_Controller_Request instance is easily passed to Zend_Navigation_Page::factory() and then to the setParams() method of the Zend_Navigation_Page_Mvc instance.


Solution

  • I have limited resources at the moment so I cannot really check my suggestion, but if you're in you controller, why don't you..

    
    $controller = $this->_request->getControllerName();
    $action = $this->_request->getActionName();
    
    $page = new Zend_Navigation_Page( array(
                       'label' => "Sonny's Page",
                       'controller' => $controller,
                       'action' => $action
    ));
    
    

    Maybe you could use this as a plugin so that you overload your view? And then add to your Navigation Container?

    Cheers,
    Angel