Zend Framework Source Code Snippets

Simple breadcrumbs view helper

Bookmark and Share
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
<?php
 
/**
 * @category   Ext
 * @package    Ext_View * @subpackage Helper
 * @author     Mokevnin Kirill <mokevnin@gmail.com>
 * @license    New BSD License
 */
 /**
 * Helper for making easy breadcrumbs
 */
class Ext_View_Helper_Breadcrumbs extends Zend_View_Helper_Abstract
{    private $_items = array();
    private $_separator = ' / ';
 
    /**
     *     * @param string $title
     * @param string $route
     * @param array $params
     * @return Ext_View_Helper_Breadcrumbs
     */    public function breadcrumbs($title = null, $route = null, array $params = array())
    {
        if (is_null($title)) {
            return $this;
        }        $item['title']  = $title;
        $item['router'] = $route;
        $item['params'] = $params;
     
        $this->_items[] = $item; 
        return $this;
    }
 
    public function render()    {
        $router = Zend_Controller_Front::getInstance()->getRouter();
        $items = array();
        foreach ($this->_items as $item) {
            if ($item['router'] == $router->getCurrentRouteName()) {                $items[] = $item['title'];
            } else {
                $url = $router->assemble($item['params'], $item['router']);
                $items[] = sprintf('<a href="%s">%s</a>', $url, $item['title']);
            }        }
        return implode($this->getSeparator(), $items);
    }
 
    public function __toString()    {
        return $this->render();
    }
 
    /**     *
     * @param string $separator
     */
    public function setSeparator($separator)
    {        $this->_separator = $separator;
    }
 
    public function getSeparator()
    {        return $this->_separator;
    }
}

Comments

Alexey Alexey
1 year ago

Line 36-37: $item wasn't initialized as an array. I think this way is much better:

$this->_items[] = array(
'title' => $title,
'router' => $route,
'params' => $params
);

You must login before commenting on a snippet. If you do not have an account, please register.

Snippet description

Ext_View_Helper_Breadcrumbs
http://github.com/mokevnin/zf-extension

#application.ini
resources.view.helperPath.Ext_View_Helper_ = BASE_PATH "/library/Ext/View/Helper"

#action
$this->view->breadcrumbs('Main page');
$this->view->breadcrumbs('About', 'about', array('uri' => 'about.html'));

#layout
<?= $this->breadcrumbs()->render() ?>

Snippet details

Created:
toxic-mt toxic-mt
1 year ago
Edited:
toxic-mt toxic-mt
1 year ago
Revision Id:
165
Edit Message:
Initial Release
ZF Version
1.10.3
Tags:
view helper
Comments:
1
Views:
4392
Points:
1 (1 votes)

History

r165

Initial Release

toxic-mt toxic-mt
1 year ago