Author Topic: Zend: Breadcrumbs and dynamically generated pages  (Read 2801 times)

0 Members and 2 Guests are viewing this topic.

Offline kalusnTopic starter

  • Irregular
  • Posts: 2
    • View Profile
Zend: Breadcrumbs and dynamically generated pages
« on: January 12, 2010, 02:27:16 PM »
I have a problem getting breadcrumbs to work properly in Zend Framework. I followed the zendcast guide on http://www.zendcasts.com/zend_navigation-dynamically-creating-a-menu-a-sitemap-and-breadcrumbs/2009/06/ in order to have a menu, sitemap and breadcrumbs generated from an .xml file.

My pages are routed by the sub route, which has the value /sub/:page/. It refers to the contentAction of the IndexController. When i run it on my localhost server, it returns an error: "Fatal error: Call to undefined method stdClass::setClass() in /var/www/mywebapp/application/controllers/IndexController.php on line 23"

The error signals that the problem is with the $activeNav object, and thus the $this->view->navigation()->findByUri($uri);. It doesn't help if i change it to findByLabel('forside'); (which is a label used in the navigation.xml file).

However, I found a comment on that told me to add Zend_Registry::set(Zend_Navigation, $navigation); after the construction of the Zend_Navigation object in the Bootstrap. Now it works! and i'm supposed to be happy, but I still don't understand what it does. Can anyone explain to me why I have to use the Zend_Registry::set(Zend_Navigation, $navigation); command. Is it because of my custom routes?

I don't have a problem anymore, but I'm very curious as to how this thing work. An explanation will be greatly appreciated.

Thank you.
 

IndexController:

public function contentAction()
{

	
/* Setting the uri to be used by breadcrumbs*/
	
$uri $this->_request->getPathInfo();    
	

	
$activeNav $this->view->navigation()->findByUri($uri);
	
$activeNav->active true;
	
$activeNav->setClass(”active”);
}

Bootstrap.php

	
protected function 
_initNavigation()
	
{
	
	
$this->bootstrap('layout');
	
	
$layout $this->getResource('layout');
	
	
$view $layout->getView();
	
	
$config = new Zend_Config_Xml(APPLICATION_PATH '/configs/navigation.xml''nav');
	
	
$navigation = new Zend_Navigation($config);
	
	
Zend_Registry::set(Zend_Navigation$navigation); // WHAT DOES THIS DOOOOOO?!??!?!?!?
	
	
$view->navigation($navigation);
	
}

navigation.xml
Code: [Select]
<?xml version="1.0" encoding="UTF-8"?>
<config>
<nav>
<home>
<label>Introduction</label>
<uri>/</uri>
<pages>
<forside>
<label>forside</label>
<uri>/sub/forside</uri>
</forside>
<produkter>
<label>produkter</label>
<uri>/sub/produkter</uri>
</produkter>
<kayloos>
<label>kayloos</label>
<uri>/sub/kayloos</uri>
</kayloos>
<kontakt>
<label>kontakt</label>
<uri>/sub/kontakt</uri>
</kontakt>
</pages>
</home>
</nav>
</config>

routes.ini:
Code: [Select]
routes.sub.type = "Zend_Controller_Router_Route"
routes.sub.route = "sub/:page/*"
routes.sub.defaults.controller = "index"
routes.sub.defaults.action = "content"

Layout script:

<div id="breadcrumbs">
	
<?= 
$this->navigation()->breadcrumbs()->setMinDepth(0)
	
	
	
	
	
	
	
->
setLinkLast(true)
	
	
	
	
	
	
	
->
setSeparator(' : ');?>
</div>

Offline ignace

  • Guru
  • Freak!
  • *
  • Posts: 5,093
  • Gender: Male
    • View Profile
Re: Zend: Breadcrumbs and dynamically generated pages
« Reply #1 on: January 12, 2010, 03:05:50 PM »
Zend_Navigation will look for an item Zend_Navigation in the registry if you don't pass it explicitly.
Developer from Belgium, Vlaams-Brabant

Offline kalusnTopic starter

  • Irregular
  • Posts: 2
    • View Profile
Re: Zend: Breadcrumbs and dynamically generated pages
« Reply #2 on: January 13, 2010, 12:52:40 PM »
Ok. I've passed it explicitly with the Zend_Registry::set(Zend_Navigation, $navigation); line, but my question is then, why does it have to be passed explicitly, when it should be in the registry automatically?

I've learned what I know about Zend mostly from trial and error through the reference guide - there might be some big basics I don't know. If you know any good reads, tell me.