I was integrating breadcrumbs into a site I am currently building and figured I would share the code I used to make it possible.
This project uses a MODULAR layout, so theres no solutions out there that helped me out. I wanted it to display cleanly, without displaying things like ‘default’ as the module name, and not to display a breadcrumb if it was the home page (ie. default module, index controller, index action).
Basically it is a view helper that you call, and it fetches everything for you.
Some things to know. It pulls ’siteurl’ from the Zend_Registry, you can change this if you want, well you can change anything you want, it’s nothing special, but it may help someone out there.
Just add this viewhelper
<?php
/**
* BreadCrumb View Helper
*@author Joey Adams
*
*/
class ViewHelpers_BreadCrumb {
public function breadCrumb() {
$module = Zend_Controller_Front::getInstance()->getRequest()->getModuleName();
$l_m = strtolower($module);
$controller = Zend_Controller_Front::getInstance()->getRequest()->getControllerName();
$l_c = strtolower($controller);
$action = Zend_Controller_Front::getInstance()->getRequest()->getActionName();
$l_a = strtolower($action);
// HomePage = No Breadcrumb
if($l_m == 'default' && $l_c == 'index' && $l_a == 'index'){
return;
}
// Get our url and create a home crumb
$url = Zend_Registry::get('siteurl');
$homeLink = "<a href='{$url}/'>Home</a>";
// Start crumbs
$crumbs = $homeLink . " > ";
// If our module is default
if($l_m == 'default') {
if($l_a == 'index'){
$crumbs .= $controller;
} else {
$crumbs .= "<a href='{$url}/{$controller}/'>$controller</a> > $action";
}
} else {
// Non Default Module
if($l_c == 'index' && $l_a == 'index') {
$crumbs .= $module;
} else {
$crumbs .= "<a href='{$url}/{$module}/'>$module</a> > ";
if($l_a == 'index') {
$crumbs .= $controller;
} else {
$crumbs .= "<a href='{$url}/{$module}/{$controller}/'>$controller</a> > $action";
}
}
}
return $crumbs;
}
}


August 29th, 2008 at 5:18 am
Hey, thanks for this.
You should use > in your strings
monk.e.boy
August 29th, 2008 at 5:37 am
ooops, i mean & g t ;
September 4th, 2008 at 7:09 am
[...] you seen this article? Breadcrumbs with Zend Framework | Joey Adams Dot Net That helped me out with my code. [...]
December 10th, 2008 at 2:58 am
[...] Breadcrumbs with Zend Framework | Joey Adams Dot Net [...]
January 9th, 2009 at 3:15 pm
Excellent helper! The only things to note for me and other people on Zend Project Numero Uno:
* The helper needed to be saved as `APPLICATION_DIR/views/helpers/BreadCrumb.php`
* The class needed to be named `Zend_View_Helper_BreadCrumb`
That might be specific to my project, but I thought it might be worth mentioning. Thanks again for such a useful helper!
January 19th, 2009 at 4:54 am
this is great!!!
January 22nd, 2009 at 6:12 am
Great, thanks… i’m having one problem though…
I don’t know to what to set the siteurl.. Do i have to set it to “http://localhost/mysite/” and change it when it goes in production?
Is there a way to do it so it does not require this base url?
February 10th, 2009 at 11:04 pm
Hello Freek,
It is better to have site url in a global file as constatnt and use it through out the application. So, in production enviormnet you just need to change the site url. Otherwise you will have headache to change the siteurl in application by going through all the pages.
Best regards,
Kalpesh
February 11th, 2009 at 9:30 am
As Kapesh said, You want a central secure repository for configurations. Preferably outside document root.
In this example you can see I was using the Zend Registry.
One idea would be to store your config options in ‘.ini’ files stored outside document root in a structured way, then use Zend_Registry to parse the ini’s and load them into the registry.
Using the method in the example, I simply set the site-url inside my bootstrap.
February 17th, 2009 at 9:05 am
Very nice helper! thanks!
Aside from renaming to “Zend_view_helper_BreadCrumb” and placing it in the application/views/helpers dir like Brian mentioned above, I suggest the following:
Instead of:
****
// Get our url and create a home crumb
$url = Zend_Registry::get(’siteurl’);
****
Replace with:
****
// Get our url and create a home crumb
$fc = Zend_Controller_Front::getInstance();
$url = $fc->getBaseUrl();
****
Thanks again for sharing!
February 17th, 2009 at 11:34 am
^ Great suggestion, I have not tested it, though. When I originally wrote this helper, it was in the early days of the ZF , and baseUrl() was a helper we had to create ourselves, so I left it out for portability under different configurations.
However,now that they (assuming) have implemented BaseUrl() into the front controller, the overhead of the registry is not needed, and it will automate the generation of the path, so hardcoding and changing the configuration between moving is not necessary.
March 24th, 2009 at 5:29 pm
I’m a beginner with ZF, but is it not supposed to extend Zend_View_Helper_Abstract?
I don’t know how to use this helper. I’ve followed the Miguel’s tip above but doesn’t work for me
April 30th, 2009 at 12:35 pm
That is wicked! Thanks!
###
@Marcelo Andrade:
How to use it:
1.Put Joey’s code into “views/helpers” directory.
2. Name it “BreadCrumb.php”
3. Instead of:
“class ViewHelpers_BreadCrumb {”
inside Joey’s code, name it:
“class Zend_view_helper_BreadCrumb {”
4. In your layout file or any view file enter this line:
breadCrumb();?>
It will display breadcrumbs.
April 30th, 2009 at 2:19 pm
^ Thank you for the step-by-step explanation
May 2nd, 2009 at 12:21 pm
In the step-by-step of @Publius…
This is the last line for me, in layout or view..
breadCrumb();?>
and Magic come true!!!!
Thanks a lot for this great helper!!!!
May 12th, 2009 at 3:53 am
Zend Framework has a built-in Breadcrumbs view helper
http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.navigation.breadcrumbs
May 12th, 2009 at 4:49 am
very cool Sam, I’m glad to see this has finally been integrated into the framework, as this was not available whenever I created this helper.