Simple Breadcrumbs Components/Helper

I made this component/helper a while ago for a site i was building and i thought i would share it with you. It’s a very simple and straight forward way to add breadcrumbs to your site with minimal mess or fuss.

Component Code (app/controllers/components/breadcrumb.php):

< ?php
/*
Copyright (c) 2009 Matthew Nessworthy - http://www.devmatt.co.za/
 
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
 
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
 
class BreadcrumbComponent extends Object {
 
  var $controller;
  var $breadcrumbs;
 
  //called before Controller::beforeFilter()
  function initialize(&$controller, $settings = array()) {
    // saving the controller reference for later use
    $this->controller =& $controller;
  }
 
  //called after Controller::beforeRender()
  function beforeRender(&$controller) {
    $this->controller->set('__breadcrumbs', $this->breadcrumbs);
  }
 
  function add() {
    $args = func_get_args();
 
    if(!$args) {
      $args = array(
        array(
          $this->controller->name,
          $this->controller->here
        )
      );
    }
 
    foreach($args AS $breadcrumb) {
      $this->breadcrumbs[] = $breadcrumb;
    }
  }
 
  function reset() {
    $this->breadcrumbs = array();
  }
 
}
 
?>

Helper Code (app/views/helpers/breadcrumb.php):

<?php
/*
Copyright (c) 2009 Matthew Nessworthy - http://www.devmatt.co.za/
 
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
 
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
 
class BreadcrumbHelper extends AppHelper {
 
  var $helpers = array(
    'Html'
  );
 
  var $breadcrumbs;
 
  function beforeRender() {
    $view =& ClassRegistry::getObject('view');
    if ($view && isset($view->viewVars['__breadcrumbs'])) {
      $this->breadcrumbs = $view->viewVars['__breadcrumbs'];
    }
  }
 
  function generate() {
    return $this->output($this->build_nav($this->breadcrumbs));
  }
 
  function build_nav($breadcrumbs, $level=0) {
    $i = 0;
    $count = count($breadcrumbs);
    $out = '
    '
; foreach($breadcrumbs AS $breadcrumb) { $i++;   $breadcrumb[1] = (isset($breadcrumb[1]) && $breadcrumb[1]) ? $breadcrumb[1] : '#'; $breadcrumb[2] = (isset($breadcrumb[2]) && $breadcrumb[2]) ? $breadcrumb[2] : null; $breadcrumb[3] = (isset($breadcrumb[3]) && $breadcrumb[3]) ? $breadcrumb[3] : null;   $out .= '
  • '
  • ;   $out .= $this->Html->link($breadcrumb[0], $breadcrumb[1], $breadcrumb[2]);   if(isset($breadcrumb[3]) && is_array($breadcrumb[3]) && $breadcrumb[3]) { $out .= $this->build_nav($breadcrumb[3], $level+1); }   $out .= '   '; }   $out .= ' ';   return $out; }   }   ?>

    Setup:

    • Create a file called breadcrumb.php in your components folder (e.g. app/controllers/components/breakcrumb.php) with the code above.
    • Create a file called breadcrumb.php in your helpers folder (e.g. app/views/helpers/breakcrumb.php) with the code above.
    • Include the Component in the controllers you want to use it in – i would suggest including it in your AppController – (e.g. var $components = array(‘Breadcrumb’);)
    • Include the Helper in the controllers you want to use it in – i would suggest including it in your AppController – (e.g. var $helpers = array(‘Breadcrumb’);)

    Usage (Controller Code):

    $this->Breadcrumb->add(
      array(
        'Home', //title
        array('controller' => 'pages', 'action' => 'home') //link - array based
      ),
      array(
        'Posts',
        '/posts/index' //link - string based,
        //dropdown links
        array(
          array(
            'Latest',
            '/posts/latest'
          )
        )
      )
    );
    OR
    $this->Breadcrumb->add(
      array(
        'Home',
        array('controller' => 'pages', 'action' => 'home')
      )
    );
     
    $this->Breadcrumb->add(
      array(
        'Posts',
        '/posts/index',
        array(
          array(
            'Latest',
            '/posts/latest'
          )
        )
      )
    );

    Usage (View Code):

    <div id="breadcrumbs">
     < ?php echo $breadcrumb->generate(); ?>
    div>

    The output generated will look a lot like this:

    My preferred menu is the excellent Superfish JS/CSS menu package