Page Breadcrumb Links in CakePHP

Posted by Mr PHP on

I wanted to make a quick and easy breadcrumb that worked with a simple array.

The breadcrumb is an array containing multiple links from html_helper::link().

The Models

For this example we need to have a category with the Tree behaviour.


class Post extends AppModel {
	var $name = 'Post';
	var $belongsTo = array(
		'Category' => array(
			'className' => 'Category',
			'foreignKey' => 'category_id',


class Category extends AppModel {
	var $name = 'Category';
	var $actsAs = array('Tree');

The Controllers

I will provide the controller action for posts::view(), however you can use this on any and every action.


class PostsController extends AppController {
	var $name = 'Posts';

	function view($id = null) {
		if (!$id) {
			$this->_flash(__('Invalid Post.', true),'error');
		// load the post
		$post = $this->Post->read(null, $id);

		// set the category path
		$post['CategoryPath'] = $this->Post->Category->getPath($post['Post']['category_id']); 
		$post['CategoryPath'] = $post['CategoryPath']?$post['CategoryPath']:array(); 

		// set page title
		$title = $post['Post']['name'];
		// set breadcrumb
		$breadcrumb = array();
		$breadcrumb[] = $html->link(__('Home',true),'/');
		$breadcrumb[] = $html->link(__('Posts',true),array('controller'=>'posts','action'=>'index'));
 		foreach ($post['CategoryPath'] as $category) {
			$breadcrumb[] = $html->link(__($category['Category']['name'],true),array(
		$breadcrumb[] = $title;

		// set page variables

The View

The view is very simple.


<?php if (isset($breadcrumb)): ?>
	<div id="breadcrumb"><?php echo implode(' &raquo; ',$breadcrumb); ?></div>
<?php endif; ?>

Tagged with : CakePHP