juworld
juworld

Reputation: 140

zf2 Navigation isActive not working with uri

I'm building menu using Navigation. In my config file, I'm using uri (I couldn't get route, controller, action to work correctly)

    'navigation' => array(
    'default' => array(
         array(
             'label'    => 'Home',
             // not sure why I can't use route, controller, action...
             // for now, i'm using uri
             'uri'      => '/',
         ),
         array(
            'label'     => 'About us',
            'uri'       => '/application/intro',
         ), ...

The application module's route is the default one, set up as such

        'application' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/application',
                'defaults' => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller'    => 'Index',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'default' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '/[:controller[/:action]]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                        ),
                    ),
                ),
            ),
        ),

Now, isActive is not working in my view (a partial) when calling

<?php foreach ($this->container as $page): ?>

    <li <?php echo $page->isActive()? 'class="' . $activeClass . '"' : '' ?>>
    ...

When I tried to do this manually, I'm trying to compare with $this->url. I realized that $this->url only returns the path to route. Meaning, in my example, when I'm in /application/intro, $this->url will only return /application/ and "intro" controller is not returned.

My question is, how do I can the url to return with controller, action, etc.? or what can I do to make sure that isActive can work correctly.

Thanks in advance!

Justin

Upvotes: 0

Views: 1555

Answers (1)

Ruben
Ruben

Reputation: 5095

From the docs at http://framework.zend.com/manual/2.0/en/modules/zend.navigation.pages.html:

Note

Zend\Navigation\Page\Uri will not try to determine whether it should be active when calling $page->isActive(). It merely returns what currently is set, so to make a URI page active you have to manually call $page->setActive() or specifying active as a page option when constructing.

So as long as you use the uri pages for your navigation this won't work. What exactly isn't working when using the routes for your navigation?

Edit: You are using the 'application' route, so if that route is defined as in your OP then it will always return /application because it is a literal route with the route property set to /application. What you probably want is to point the navigation item to the child route of the application route, so what if you try to set the route to application/default instead?

Upvotes: 1

Related Questions