Breadcrumb
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
| <?php
<?php
/**
* @copyright 2009, S. Mohammed Alsharaf
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU Public License * @author S. Mohammed Alsharaf (satrun77@hotmail.com)
* @link http://jamandcheese-on-phptoast.com/
* @version 2.1
*/
class My_View_Helper_Layout_Breadcrumb extends Zend_View_Helper_Abstract
{
protected $_adapter = null;
protected $_seperator = \'\';
protected $_data = array(); protected $_frontController = null;
protected $_module = \'\';
protected $_controller = \'\';
protected $_action = \'\';
protected $_baseUrl = \'\'; const MODULE = 0;
const CONTROLLER = 1;
const ACTION = 2;
public function __construct() {
$this->_frontController = Zend_Controller_Front::getInstance();
$this->_module = $this->_frontController->getRequest()->getModuleName();
$this->_action = $this->_frontController->getRequest()->getActionName();
$this->_controller = $this->_frontController->getRequest()->getControllerName(); $this->_baseUrl = $this->_frontController->getBaseUrl() . \'/\';
}
public function breadcrumb($adapter=\'MVC\', $replacement=null, $seperator=\'>\')
{ if($this->_module == \'default\' && $this->_controller == \'index\' && $this->_action ==\'index\') {
return \'\';
} elseif($this->_controller == \'error\') {
return \'\';
}
if(is_array($adapter)) {
$this->setData($adapter);
$this->setSeperator($seperator);
return $this->render(); }
$adapterName = __CLASS__ . \'_\' . ucfirst($adapter);
$this->_adapter = new $adapterName;
if (!$this->_adapter instanceof My_View_Helper_Layout_Breadcrumb_Interface) {
throw new Exception(\'Adapter name is invalid\');
}
$this->_adapter->setSeperator($seperator); $this->_adapter->prepare();
if(!is_null($replacement)) {
$this->_adapter->replace($replacement);
}
return $this->_adapter->render(); }
protected function setSeperator($seperator)
{
$this->_seperator = $seperator; return $this;
}
protected function setData($data)
{ $this->_data = $data;
return $this;
}
protected function replace(array $replacement) {
foreach($replacement as $section => $data) {
if(is_array($data)) {
foreach($data as $key => $value) {
$this->_data[$section][$key] = $value; }
} else {
$this->_data[$section] = $data;
}
} return $this;
}
protected function render()
{ ksort($this->_data);
$breadcrumb = \'<div class=\"breadcrumb\"><a href=\"\' . $this->_baseUrl . \'\">Home</a> > \';
foreach($this->_data as $key => $value) { $breadcrumb .= $this->renderItem($key);
}
$breadcrumb .= \'</div>\';
return $breadcrumb; }
private function renderItem($type)
{
$breadcrumb = \'\'; if(isset($this->_data[$type])) {
if(isset($this->_data[$type][\'url\'])) {
$breadcrumb .= \'<a href=\"\' . $this->_data[$type][\'url\'] . \'\">\' . $this->_data[$type][\'title\'] . \'</a>\';
$breadcrumb .= \'<span>\' . $this->_seperator . \'</span>\';
} elseif(isset($this->_data[$type][\'title\'])) { $breadcrumb .= \'<span>\' . $this->_data[$type][\'title\'] . \'</span>\';
}
}
return $breadcrumb; }
} |
1
2
3
4
56
| <?php
interface My_View_Helper_Breadcrumb_Interface
{
public function prepare();} |
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
| <?php
class My_View_Helper_Layout_Breadcrumb_MVC
extends My_View_Helper_Layout_Breadcrumb
implements My_View_Helper_Layout_Breadcrumb_Interface{
protected function getTitle($string)
{
$string = str_replace(\"-\" , \" \" , $string);
$string = ucfirst($string); return $string;
}
/**
* Breadcrumb in default module. we dont include the module name *
*/
protected function inDefaultModule()
{
$data = array(); if($this->_action == \"index\" && $this->_controller == \"index\") {
$this->_data = $data;
return;
}
if($this->_action == \"index\") {
$data[self::CONTROLLER][\'title\'] = $this->_controller == \'index\'? \'home\' : $this->getTitle($this->_controller);
$this->_data = $data;
} else {
$data[self::CONTROLLER][\'url\'] = $this->_baseUrl . $this->_controller; $data[self::CONTROLLER][\'title\'] = $this->getTitle($this->_controller);
$data[self::ACTION][\'title\'] = $this->_action == \'index\'? \'home\' : $this->getTitle($this->_action);
$this->_data = $data;
}
}
protected function inCustomModule()
{
$data = array();
$data[self::MODULE][\'url\'] = $this->_baseUrl . $this->_module; $data[self::MODULE][\'title\'] = $this->getTitle($this->_module);
if($this->_controller == \"index\") {
unset($data[self::MODULE][\'url\']);
//$data[self::CONTROLLER][\'title\'] = $this->_controller == \'index\'? \'home\' : $this->getTitle($this->_controller); } else {
$data[self::CONTROLLER][\'url\'] = $this->_baseUrl . $this->_module . \'/\'. $this->_controller;
$data[self::CONTROLLER][\'title\'] = $this->getTitle($this->_controller);
if($this->_action == \'index\') {
unset($data[self::CONTROLLER][\'url\']); } else {
$data[self::ACTION][\'title\'] = $this->getTitle($this->_action);
}
}
$this->_data = $data;
}
public function prepare() {
if($this->_module == \"default\") {
$this->inDefaultModule();
} else {
$this->inCustomModule(); }
}
}
Example one: uses mvc class to creates the breadcrumb |
1
2
3
| echo $this->breadcrumb();
Example two: |
1
2
3
4
56
7
8
9
1011
12
13
| $data = array(
\'0\' => array(
\'title\' => \'About\',
\'url\' => \'about-us\'
), \'1\' => array(
\'url\' => \'my-url\',
\'title\' => \'title\'
)
);echo $this->breadcrumb($data, \'>\');
Example three: |
1
2
3
4
| /**
* say it output contact us : support
*/
echo $this->breadcrumb(\'mvc\'); |
1
2
3
4
56
7
8
9
10 | /**
* same example as the above one.
* here want to change the controller support to New Supports. then pass an array as the second argument and it will replace value of the controller
*/
$data = array( \'1\' => array(
\'title\' => \'New Supports\'
)
);
echo $this->breadcrumb(\'mvc\', $data); |
Comments
You must login before commenting on a snippet. If you do not have an account,
please register.
Snippet description
Generate breadcrumb
Snippet details
- Created:
-
satrun77
2 years ago
- Edited:
-
satrun77
1 year ago
- Revision Id:
- 163
- Edit Message:
- Changed author url
- ZF Version
- 1.10.3
- Tags:
- view helper
breadcrumb
view
helper
- Comments:
- 2
- Views:
- 1804
- Points:
- 1 (1 votes)
History
2 years ago
A breadcrumb view helper is an extremely useful idea, but I think this implementation is severely lacking. Even if you put aside for a moment that this view helper requires the MVC components of Zend Framework to run, this is making the quite dramatic assumption that the module/controller/action route will be persistent throughout this entire use case and that custom routes will not be used at all. Not only that, but the names of the module, controller, and actions will also be the exact name that you want to display to the user. I can't speak for anyone but myself, but I can't really imagine a production website where that would be the case.