Search code examples
model-view-controllerzend-frameworkzend-navigation

Why does Zend mark a subpage as in-active?


I'm having issues setting up my breadcrumbs and menu using Zend_Navigation.

First I set up my pages using a XML config object:

<?xml version="1.0" encoding="UTF-8"?>
<configdata>
<nav>
    <home>
        <label>Home</label>
        <controller>Index</controller>
        <action>index</action>
        <id>home</id>
        <resource>default</resource>
    </home>
    <crm>
        <label>CRM</label>
        <module>Crm</module>
        <controller>Index</controller>
        <action>index</action>
        <id>crm</id>
        <resource>Crm</resource>
        <pages>
            <persons>
                <module>Crm</module>
                <label>Personen</label>
                <controller>Persons</controller>
                <action>index</action>
            </persons>
           (...)etc.(...)

Then in my bootstrap:

//Bootstrap.php
$view = $layout -> getView();
$config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'nav');
$navigation = new Zend_Navigation($config);
$view -> navigation($navigation);
$view -> menu = $view -> navigation() -> menu();
$view -> breadcrumbs = $view -> navigation()->breadcrumbs()->setMinDepth(0);

Now if I was to navigate to http://hostname/Crm/Persons/ the active state would work and the breadcrumbs would also display correctly.

However when I go to http://hostname/Crm/Persons/inspect/id/3 (where inspect is the action and id is a parameter) the breadcrumbs will be empty and none of the menu items will have an active state. The expected breadcrumbs are something like: Home > CRM > Personen > John and CRM and Personen should be active in the menu.

Now Zend documentation gave me a clue: it might not be working because of the parameter being set.

/*
* Dispatched request:
* - module:     blog
* - controller: post
* - action:     view
*/
$page = new Zend_Navigation_Page_Mvc(array(
    'action'     => 'view',
    'controller' => 'post',
    'module'     => 'blog',
    'params'     => array('id' => null)
));

// returns false, because page requires the id param to be set in the request
$page->isActive(); // returns false

I have no idea how to fix this however. Thoughts are highly appreciated.


Solution

  • After a some (a lot) of tinkering, I figured out I needed to define the page within the XML before Zend would recognize the structure.

    <?xml version="1.0" encoding="UTF-8"?>
    <configdata>
    <nav>
        <home>
            <label>Home</label>
            <controller>Index</controller>
            <action>index</action>
            <id>home</id>
            <resource>default</resource>
        </home>
        <crm>
            <label>CRM</label>
            <module>Crm</module>
            <controller>Index</controller>
            <action>index</action>
            <id>crm</id>
            <resource>Crm</resource>
            <pages>
                <persons>
                    <module>Crm</module>
                    <label>Personen</label>
                    <controller>Persons</controller>
                    <action>index</action>
                    <pages>
                        <inspect>  <--- this will make Zend recognize the page
                            <module>Crm</module>
                            <label>Persoon</label>
                            <controller>Persons</controller>
                            <action>inspect</action>
                        </inspect>
                    </pages>
                </persons>
                (...)etc(...)
    

    Note that I didn't want the inspect action to show up on the menu and set the max render depth to 1:

    $view -> menu = $view -> navigation() -> menu()->setMaxDepth(1);