Page Breadcrumb Links in CakePHP

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.

models/post.php
<?php
class Post extends AppModel {
	var $name = 'Post';
	var $belongsTo = array(
		'Category' => array(
			'className' => 'Category',
			'foreignKey' => 'category_id',
		),
	);                
}
?>
models/category.php
<?php
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.

controllers/posts_controller.php
<?php
class PostsController extends AppController {
	var $name = 'Posts';

	function view($id = null) {
		if (!$id) {
			$this->_flash(__('Invalid Post.', true),'error');
			$this->redirect(array('action'=>'index'));
		}
		
		// 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(
				'controller'=>'posts',
				'action'=>'index',
				'category_id'=>$category['Category']['id']
			));
		}
		$breadcrumb[] = $title;

		// set page variables
		$this->set(compact('post','title','breadcrumb'));
	}
}
?>

The View

The view is very simple.

views/layout/default.ctp
<?php if (isset($breadcrumb)): ?>
	<div id="breadcrumb"><?php echo implode(' » ',$breadcrumb); ?></div>
<?php endif; ?>

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <b> <i> <strong> <cite> <em> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <css>, <diff>, <drupal5>, <html>, <javascript>, <php>. The supported tag styles are: <foo>, [foo]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.