1

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

herrjeh42
  • 2,782
  • 4
  • 35
  • 47

2 Answers2

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());
}
Daimon
  • 3,703
  • 2
  • 28
  • 30
  • Then use Plugin to dynamically add Zend_Navigation_Page's based on database data – Daimon Nov 08 '10 at 11:28
  • Do you have an online resource by hand as a starting point how to write this plugin? – herrjeh42 Nov 08 '10 at 12:07
  • I've added example Navigation plugin in my answer. Maybe it's not the common way but I find it very flexible to do any custom navigation. – Daimon Nov 08 '10 at 13:29
  • Daimon, thanx so much!! I will post my solution (based on yours as a separate posting). My communication was not very clear again... I don't have the complete navigation in the database, but some dynamic parts are retrieved from the database, transfered as url parameters and are used in links. With your example, I could finally work out my solution (feel free to improve it btw). :-) – herrjeh42 Nov 08 '10 at 15:50
1

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
herrjeh42
  • 2,782
  • 4
  • 35
  • 47