Zend Framework

Using Zend_Navigation combined with a route href problem

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Not an Issue
  • Affects Version/s: 1.8.0
  • Fix Version/s: None
  • Labels:
    None

Description

I'm using the Zend_Navigation to build a menu, this is build in the bootstrap file.
One of the pages go's to a route, when i'm in that routed page al href's of the pages into my menu go's to that route and not the right module / controller / view..

Here's the code

$pages = array(
	array(
		'label' => 'Company',
		'title' => 'About us',
		'module' => 'newmodule',
		'controller' => 'index',
		'action' => 'index',
		'pages' => array(
			array(
				'label' => 'News',
				'class' => 'rss', // class
				'module' => 'newmodule',
				'controller' => 'index',
				'action' => 'index',
				'pages' => array(
					array(
						'label' => 'Archive',
						'route' => 'blaat', // route
						'module' => 'newmodule',
						'controller' => 'index',
						'action' => 'hallow',
						'params' =>array("year"=>"2009")
					)
				)
			)
		)
	),
);
$view->navigation()->addPages($pages);


$router = $frontController->getRouter();
$router->addDefaultRoutes();
$router->addRoute(
	'blaat',
	new Zend_Controller_Router_Route(
		'/archive/:year',
		array(
			'module' => 'newmodule',
			'controller' => 'index',
			'action' => 'hallow',
			'year' => (int) date('Y') - 1
		),
		array('year' => '\d+')
	)
);

Issue Links

Activity

Hide
Deleted added a comment -

I have the problem with the $this->navigation()->menu(); function
When I first call $page1->getHref(); and then the menu function the problem don't appears

Show
Deleted added a comment - I have the problem with the $this->navigation()->menu(); function When I first call $page1->getHref(); and then the menu function the problem don't appears
Hide
Robin Skoglund added a comment -

This is probably because you haven't specified any route for the other pages. If I recall correctly, the Zend_Controller_Action_Helper_Url (which the component uses to generate URLs) will use the currently active route if null is given as $route in the call to $helper->url($params, $route, ...).

Does it work as expected if you specify 'default' as module for the other pages?

Show
Robin Skoglund added a comment - This is probably because you haven't specified any route for the other pages. If I recall correctly, the Zend_Controller_Action_Helper_Url (which the component uses to generate URLs) will use the currently active route if null is given as $route in the call to $helper->url($params, $route, ...). Does it work as expected if you specify 'default' as module for the other pages?
Hide
Deleted added a comment -

Yes this work's thanks!

But... It is a little strange because the code I uses is from the documentation @ http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.navigation.setup

Maybe it's a idee if the 'route' is not specified the value automatic is default?

Show
Deleted added a comment - Yes this work's thanks! But... It is a little strange because the code I uses is from the documentation @ http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.navigation.setup Maybe it's a idee if the 'route' is not specified the value automatic is default?
Hide
Robin Skoglund added a comment -

That was the case originally, but it was changed to null because many users reported that they did not use the 'default' route, or changed it to something else.

I'll try and add a note about this in the manual for the next mini release.

Show
Robin Skoglund added a comment - That was the case originally, but it was changed to null because many users reported that they did not use the 'default' route, or changed it to something else. I'll try and add a note about this in the manual for the next mini release.
Hide
Deleted added a comment -

Oke, thank you for this information

Show
Deleted added a comment - Oke, thank you for this information
Hide
Igor Fridman added a comment -

I have exactly the same problem. Why is this "Not an Issue"? Can someone please explain?

Show
Igor Fridman added a comment - I have exactly the same problem. Why is this "Not an Issue"? Can someone please explain?
Hide
Robin Skoglund added a comment -

Because this is the way the routing system works. If you don't specify a route to the URL helper (which uses router's assemble() method), it will use assume the currently active route should be used for assembling the URL.

As a concequence of this – if you use more than one module in your application – you have to specify which route each page in Zend_Navigation should be using, because it has no way of knowing itself.

In the earliest versions of Zend_Navigation (when it was still Zym_Navigation), pages used 'default' as a default route. This "solved" the issue described here, but it causes problems for people not using a default route, or has a default route that is not named 'default'. That's why the default route was dropped from pages in Zend_Navigation, to be true to the ZF spirit of not imposing restrictions or lay assumptions on application structure.

Hope this helped clarify. If not, just ask. It should also be said that I do see how it would be useful to have the default route specified, since it would mean less configuration. This is something we have to discuss in the community, and get feedback from the zenders (Matthew).

Show
Robin Skoglund added a comment - Because this is the way the routing system works. If you don't specify a route to the URL helper (which uses router's assemble() method), it will use assume the currently active route should be used for assembling the URL. As a concequence of this – if you use more than one module in your application – you have to specify which route each page in Zend_Navigation should be using, because it has no way of knowing itself. In the earliest versions of Zend_Navigation (when it was still Zym_Navigation), pages used 'default' as a default route. This "solved" the issue described here, but it causes problems for people not using a default route, or has a default route that is not named 'default'. That's why the default route was dropped from pages in Zend_Navigation, to be true to the ZF spirit of not imposing restrictions or lay assumptions on application structure. Hope this helped clarify. If not, just ask. It should also be said that I do see how it would be useful to have the default route specified, since it would mean less configuration. This is something we have to discuss in the community, and get feedback from the zenders (Matthew).
Hide
Igor Fridman added a comment -

Specifing <route>default</route> for all the other pages worked for me. What is the difference between null and 'default' route and why Zend_Controller_Action_Helper_Url uses the currently active route?

Show
Igor Fridman added a comment - Specifing <route>default</route> for all the other pages worked for me. What is the difference between null and 'default' route and why Zend_Controller_Action_Helper_Url uses the currently active route?
Hide
Robin Skoglund added a comment -

null means "use the currently active route"
'default' means "use the actual route instance named 'default'"

...as it should be, really.

Show
Robin Skoglund added a comment - null means "use the currently active route" 'default' means "use the actual route instance named 'default'" ...as it should be, really.
Hide
Igor Fridman added a comment -

Thanks for answering my questions. It would be very useful to add a note under "Example 37.4. Using routes" describing this issue.
http://framework.zend.com/manual/en/zend.navigation.pages.html

Show
Igor Fridman added a comment - Thanks for answering my questions. It would be very useful to add a note under "Example 37.4. Using routes" describing this issue. http://framework.zend.com/manual/en/zend.navigation.pages.html
Hide
Vlatko Basic added a comment -

I don't think it is a good idea to change the behavior so much. There should have been made a solution to specify a "default" route. This way, neither the "Default" route can be used without much configuration, neither some other differently named default route.

Here is a simple solution that reverts the old behavior, for "menu" and "url" helpers.

class Old_Controller_Router_Rewrite extends Zend_Controller_Router_Rewrite {

  public function assemble($userParams,$name=null,$reset=false,$encode=true){
    if ($name == null) {
      $name = 'default';
    }
    return parent::assemble($userParams, $name, $reset, $encode);
  }

}

Something similar should be done in next ZF release (before 2.0 )

Show
Vlatko Basic added a comment - I don't think it is a good idea to change the behavior so much. There should have been made a solution to specify a "default" route. This way, neither the "Default" route can be used without much configuration, neither some other differently named default route. Here is a simple solution that reverts the old behavior, for "menu" and "url" helpers.
class Old_Controller_Router_Rewrite extends Zend_Controller_Router_Rewrite {

  public function assemble($userParams,$name=null,$reset=false,$encode=true){
    if ($name == null) {
      $name = 'default';
    }
    return parent::assemble($userParams, $name, $reset, $encode);
  }

}
Something similar should be done in next ZF release (before 2.0 )

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: