开发者

Create a Zend_Navigation_Page_Mvc instance from a Zend_Controller_Request

I have page开发者_运维技巧s 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.


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


Since we don't know the code you have written already, I'm only guessing…

You need to:

  • retrieve the actual Zend_Navigation container used in navigation() view helper
  • create new Zend_Navigation_Page instance from array of data retrieved from the request
  • add the page to the container
  • assign the new container to the navigation helper

This should be easy. The rest you need to know:

  • how to write controller plugin with preDispatch method, and put the above there,
  • how to access current view instance in this plugin (from the view renderer or from application resource/bootstrap)

Then in the plugin you operate on navigation view helper as usual in the view.

Hope this clarified some things.


Why not store the instance in Zend_Registry and then in a postDispatch from either a plugin, module bootstrap or action controller add the pages to the original nav?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜