Passing url parameters to Zend_Navigation using an XML-file

别来无恙 提交于 2019-12-01 11:07:36

问题


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".


回答1:


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());
}



回答2:


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


来源:https://stackoverflow.com/questions/4123300/passing-url-parameters-to-zend-navigation-using-an-xml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!