Passing url parameters to Zend_Navigation using an

2019-02-20 17:24发布

I am using Zend Framework 1.10.8.

I want to create a breadcrumb section in my layout.phtml. There are some links in my menu that have dynamic url parameters like http://mydomain.com/editor/edit/id/42

I try to figure out how to pass id=XXX to Zend_Navigation, while XXX comes from the database and is different in every request.

One solution I found so far is adding a property e.g. params_id to my xml declaration:

in configs/navigation.xml

<pages>
   <editor>
     <label>Editor</label>
     <controller>editor</controller>
      <action>edit</action>
     <params_id>id</params_id>
     <route>default</route>  
  </editor>
 </pages>

and in the controller looping through the pages and dynamically adding my parameter id = 42 (while 42 would be retrieved from the request object in the final version)

$pages = $this->view->navigation()->getContainer()->findAllBy('params_id','id');
            foreach ($pages as &$page) {
                $page->setParams(array(
                    'id' => 42,
                    'something_else' => 667

                ));
 }

As adding dynamic url parameters seems such a basic requirement for Zend_Navigation I am quite sure that my solution is too complicate, too expensive and there must be a much simplier solution "out of the box".

2条回答
老娘就宠你
2楼-- · 2019-02-20 18:13

It is very simple. Just write in your XML

<pages>
    <editor>
        <label>Editor</label>
        <controller>editor</controller>
        <action>edit</action>
        <params>
            <id>42</id>
            <someting_else>667</something_else>
        </params>
        <route>default</route>  
    </editor>
</pages>

Here is example to do it dynamically based on database data

First define Navigation loading plugin. Name the file Navigation.php and place it in application/plugins/ directory. Here's an example of such plugin:

class Plugin_Navigation extends Zend_Controller_Plugin_Abstract
{
    function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $view = Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer')->view;

        //load initial navigation from XML
        $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml','nav');
        $container = new Zend_Navigation($config);

        //get root page
        $rootPage = $container->findOneBy('sth', 'value');

        //get database data
        $data = Model_Sth::getData();

        foreach ($data as $row) {
            $rootPage->addPage(new Zend_Navigation_Page_Mvc(array(
                'module'     => 'default',
                'controller' => 'examplecontroller',
                'action'     => 'exampleaction',
                'route'      => 'exampleroute',
                'label'      => $row['some_field'],
                'params'     => array(
                    'param1' => $row['param1'],
                    'param2' => $row['param1']
                )
            )));
        }

        //pass container to view
        $view->navigation($container);
    }
}

Then in you Bootstrap init this plugin

protected function _initNavigation() {
    Zend_Controller_Front::getInstance()->registerPlugin(new Plugin_Navigation());
}
查看更多
干净又极端
3楼-- · 2019-02-20 18:15

An update: I finally ended up throwing away the xml file. What I do now:

  • I wrote a plugin (see Daimon's approach)
  • in this plugin I configure my navigation structure as an array, the dynamic parameters are retrieved from Zend_Request
  • then I init the navigation using this array
查看更多
登录 后发表回答