Studyon Minte9.com
ZF 1.0




Breadcrumbs



#Application/layouts/frontend.phtml
<?php 
	$this->headLink()->appendStylesheet(_APP_URL . "/lib/css/main.css");
	$this->headLink()->appendStylesheet(_APP_URL . "/lib/css/breadcrumbs.css");	
?>
<html>
<head>
	<?php echo $this->headLink()."n" ?>
</head>
<body>
	You are here: <?php echo $this->breadcrumbs()->render(); ?>

	<!--
		http://zfsite/test
		You are here:  Zfsite (/) > Admin (/admin) > Test (no link)
	-->
</body>
</html>


#Application/modules/default/controller/FrontendController.php
<?php
class FrontendController extends Zfsite_Controller_Action 
{
	public function testAction() {		
		$this->view->breadcrumbs()
			->add('Admin', 'admin')
			->add('Test', 'test', array('dd'=>44))
			//->add('Test2', 'test2') // error (no route test2 in routes.ini)
		;
	}
}

#Application/library/Zfsite/Controller/Action.php
<?php
class Zfsite_Controller_Action extends Zend_Controller_Action {
	
	public function init() {
		Zend_Layout::getMvcInstance()->setLayout("frontend");
		
		$this->view->breadcrumbs()->setSeparator('<li> > </li>');
		$this->view->breadcrumbs()->add('Zfsite', 'homepage');
	}
}


#html/index.php
<?php
...
$layout->getView()->addHelperPath('Zfsite/View/Helper/', 'Zfsite_View_Helper');


#Application/library/Zfsite/View/Helper/Breadcrumbs.php
<?php
class Zfsite_View_Helper_Breadcrumbs extends Zend_View_Helper_Abstract
{
	private $_items = array();
	private $_separator = ' / ';

	public function breadcrumbs() 
	{
		return $this;
	}

	public function add($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[] = sprintf('<li>%s</li>', $item['title']);
			} else {
				$url = $router->assemble($item['params'], $item['router']);
				$items[] = sprintf('<li><a href="%s">%s</a></li>', $url, $item['title']);
			}
		}

		$this->view->placeholder('breadcrumbs')->captureStart();
		?>
			<div class='breadcrumbs'>
			<ul>
				<?php echo implode($this->getSeparator(), $items); ?>
			</ul>
			</div>
		<?php
		$this->view->placeholder('breadcrumbs')->captureEnd();

		return $this->view->placeholder('breadcrumbs');
	}

	public function __toString()
	{
		return $this->render();
	}

	public function setSeparator($separator)
	{
		$this->_separator = $separator;
	}

	public function getSeparator()
	{
		return $this->_separator;
	}
}


#Application/config/routes.ini
;HOMEPAGE
routes.homepage.route = ""
routes.homepage.defaults.module = "default"
routes.homepage.defaults.controller = "frontend"
routes.homepage.defaults.action = "index"
;TEST
routes.test.route = "test/:page"
routes.test.defaults.module = "default"
routes.test.defaults.controller = "frontend"
routes.test.defaults.action = "test"
routes.test.defaults.page = 1
;ADMIN
routes.admin.route = "admin"
routes.admin.defaults.module = "default"
routes.admin.defaults.controller = "admin"
routes.admin.defaults.action = "index"


http://www.zfsnippets.com/snippets/view/id/87
http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.navigation.breadcrumbs
http://developer.yahoo.com/ypatterns/navigation/breadcrumbs.html