Basic breadcrumb view helper
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 39 4041 42 43 44 4546 47 48 49 5051 52 53 54 5556 57 58 59 6061 62 63 64 6566 67 68 69 7071 72 73 74 7576 77 78 79 8081 82 83 84 8586 87 88 89 9091 92 93 94 9596 97 98 99 100101 102 103 104 105106 107 108 109 110111 112 113 114 115116 117 118 119 120121 122 123 124 125126 127 128 129 130131 132 133 134 135136 137 138 139 140141 142 143 144 145146 147 148 149 | <?php /** * Breadcrumb view helper * * This helper is automated and uses the * current module, controller & action to * generated the breadcrumb. A custom breadcrumb * can be specified, using the set method. * * @todo Refactor code to allow building of a "path" * to current location. Use this in conjuction with * "path building" idea to subcategorise controllers * and still have a nice breadcrumb trail. * * @author Steven Bakhtiari <steven@ctisn.com> * @licence Use at will - no strings. */ class My_View_Helper_BreadCrumb extends Zend_View_Helper_Abstract { /** * Request Object * * @var Zend_Controller_Request_Abstract */ protected $_request; /** * Breadcrumb separator * * @var string */ protected $_separator = '›'; /** * Breadcrumb * * @var array */ protected $_breadcrumb = array(); /** * Constructor * * @return void */ public function __construct() { $fc = Zend_Controller_Front::getInstance(); $this->_request = $fc->getRequest(); } /** * Set the breadcrumb separator * * @param string $separator */ public function setSeparator($separator) { $this->_separator = $separator; } /** * Set custom breadcrumb * * @param array $breadcrumb * @return My_View_Helper_Breadcrumb */ public function set(array $breadcrumb) { $this->_breadcrumb = $breadcrumb; return $this; } /** * breadcrumb * * @param array $breadcrumb Set a custom breadcrumb * @return My_View_Helper_Breadcrumb */ public function breadcrumb(array $breadcrumb = array()) { if (empty($this->_breadcrumb)) { if (!empty($breadcrumb)) { $this->set($breadcrumb); } else { $module = $this->_request->getModuleName(); $controller = $this->_request->getControllerName(); $action = $this->_request->getActionName(); if ($module != 'default') { $this->_breadcrumb[] = array( 'title' => $module, 'url' => $this->view->url(array('module' => $module), 'default', true) ); } if ($controller != 'index') { $this->_breadcrumb[] = array( 'title' => $controller, 'url' => $this->view->url(array('module' => $module, 'controller' => $controller), 'default', true) ); } if ($action != 'index') { $this->_breadcrumb[] = array( 'title' => $action, 'url' => $this->view->url(array('module' => $module, 'controller' => $controller, 'action' => $action), 'default', true) ); } $this->_breadcrumb[count($this->_breadcrumb) - 1]['url'] = null; } } return $this; } /** * Compile and output the breadcrumb * * @return string */ public function __toString() { if (count($this->_breadcrumb) == 1) { $breadcrumb = ''; } else { $breadcrumb = '<ol class="breadcrumb">'; foreach ($this->_breadcrumb as $i => $bc) { $breadcrumb .= '<li>' . ($i != 0 ? '<span>' . $this->_separator . '</span>' : null); if ($bc['url'] === null) { $breadcrumb .= $this->view->escape($bc['title']); } else { $breadcrumb .= '<a href="' . $bc['url'] . '">' . $this->view->escape($bc['title']) . '</a>'; } $breadcrumb .= '</li>'; } $breadcrumb .= '</ol>'; } return $breadcrumb; } } |
1 2 3 | In your view script <?php echo $this->breadcrumb(); ?> |
Comments
Thanks for the comments! I will certainly take some of your suggestions onboard specifically regarding the add functionality and implementing specific title and url methods. You're right too, allowing users to define the classname would be a simple, but positive improvement.
I didn't consider other use-case scenarios (for example, using the helper outside of a typical ZF application) - but I do appreciate the comments and suggestions. Thanks! =]
Not sure if anyone else had this problem, but I had to add:
public function setView(Zend_View_Interface $view)
{
$this->view = $view;
}
...in order to make $this->view->url(...) work.
Hi bachya,
You shouldn't have to do this at all. My class extends Zend_View_Helper_Abstract which has a setView method and defines $this->view.
Perhaps it didn't in earlier versions of ZF? I think this code should definitely work with ZF 1.6 +. I'm currently using 1.7.6.
Regards
Thanks Steven, great helper.
A couple of things though:
Just in case someone prefers to use other default module, controller and action names, you could get those names as well with:
Zend_Controller_Front::getInstance()->getDefaultModule(),
Zend_Controller_Front::getInstance()->getDefaultControllerName() and
Zend_Controller_Front::getInstance()->getDefaultAction()
instead of hardcoding them to 'default' and 'index'. Of course, this would introduce another reference to Zend_Controller_Front, something that Court Ewing wishes gone. But since I plan to use this with the FrontController, I have no problem with that for now.
Another thing I noticed is that it might be good to add append 'return $this;' to the setSeparator() method, to provide a fluent interface and allow this code in the view:
<?= $this->breadcrumb()->setSeparator('-'); ?>
This will render the crumbs with the new separator.
Thanks zwippie!
You're 100% correct about returning $this from setSeparator, shouldn't have missed that. I've got it in the set method, so it should be there too - my mistake!
As for the default module and controllers and index actions, I'm not hardcoding the names with this helper, I'm checking if they _are_ the current module/controller/action names. If they are, I don't add them to the breadcrumb; if I had a blog module and I was viewing the index action in the index controller, I wouldn't want to see blog > index > index
With that said, the more time passes since I wrote the helper, the more I feel it needs to be totally re-written to allow totally customiseable paths. I will be re-writing it for the project I'm currently working on and when I do, I'll post the changes. :)
Thanks again for the comments, I'm glad the helper came in useful for anyone.
You must login before commenting on a snippet. If you do not have an account, please register.
Snippet description
This view helper will generate an ordered list of links for your breadcrumb, showing the current module, controller and action. If the controller / action is index, they're omitted.
This class assumes you're using ZF's autoloading functionality - I haven't done a require_once on Zend_View_Helper_Abstract. If you don't use the autoloading functionality of ZF, make sure you include the necessary files.
This class also makes use of the url helper function.
This probably isn't the prettiest method, so if you think I could do something in a nicer way, please say! I've just started using ZF, so any input is appreciated. I want to update my current project to implement a system where I can build paths to locations and use those for the breadcrumbs, so I can more nicely categorise controllers (eg user-management > users / user-management > permissions.
How does everyone else implement this stuff?
Snippet details
- Created:
-
Steven
- Edited:
-
Steven
- Revision Id:
- 25
- Edit Message:
- Initial Release
- Tags:
- breadcrumb view_helper view helper
- Comments:
- 7
- Views:
- 853
- Points:
- 0 (0 votes)
1 year ago
I like this implementation a lot more than the breadcrump view helper that was posted earlier; I just have a few small suggestions:
First, being able to at least customize the class name of the list would be nice. I'm sure "breadcrumb" would work for most people, but you never know!
Second, it's awesome that you've given people the ability to pass in their own breadcrumb values -- in theory, this would mean that people would be able to use your view helper even if they weren't using the Zend Framework MVC components. However, your constructor still tries to retrieve the current request from Zend_Controller_Front which may not exist at all! So while your breadcrumb function would lead me to believe I could use your view helper in any standalone application, the whole thing would fail when I tried to instantiate the view helper (or when I tried to use it for the first time, if I were using the plugin loader).
Finally, you may consider writing a breadcrumb element interface to be packaged with this breadcrumb view helper and adding an "add" function as well. The interface would, at the very least, define getTitle() and getUrl(). This way people could create their own breadcrumb elements to be added to the list (perhaps from an xml sitemap). Sure, they could simply make sure they are passing an array with both 'title' and 'url' set, but assumptions like these are generally a bad idea when you're talking about serving a helper like this to the greater populous of developers. This would require rewriting your breadcrumb function slightly so that you could call the add function multiple times prior to echoing your breadcrumb out and without accidentally forcing the Zend_Controller_Front fallback that you have set up.
Perhaps you could make it so a request object can be explicitly passed to this view helper in order for it to utilize it? That would solve a lot of problems.