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

Post new comment

  • Allowed HTML tags: <b> <br> <p> <a> <strong> <cite> <em> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • You may use [img:xx] tags to display uploaded files or images inline.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <css>, <diff>, <drupal5>, <html>, <javascript>, <php>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options