CakePHP Quick Tip: $html->clearCrumbs

Published: on 16/10/08 | Comments (0)

Hi All

As part of an ongoing project, I was using the CakePHP's htmlHelper to generate breadcrumbs.

Now combined with the tree behavior's getpath function, the $html->addCrumb() and $html->getCrumbs() functions are great and a real time saver, but...

...for this particular page I actually want to show two or more different bread crumb trails, one for location, and one or more for categories, and that's where I hit a snag, as it stands the htmlhelper allows you to keep adding crumbs and displaying the trail so far, but has no way of clearing the protected _crumbs array, should you need to make more than one trail.

AppHelper to the rescue

Since the app_helper file was added to CakePHP you now have the facility to add additional functionality to built in helpers so by adding the following code to app_helper.php:

    /**
     * enhancement to htmlHelper which allows the crumbs protected array
     * to be cleared so that more than one set of crumbs can be generated in the same view.
     * 
     * @author Peter Butler peter@studiocanaria.com
     * @return void
     * @access public
     */
    function clearCrumbs(){
        $this->_crumbs = array();
    }

You can now use $html->clearCrumbs() to clear down and build a second, third or however many breadcrumb trails you need.

Hope you find this usefull,

till the next time.


Comments

Have Your Say

Comments are now closed for this article


About Studio Canaria

Studio Canaria is the web site of freelance web developer, Peter Butler. Articles on this site relate to designing, developing and marketing modern web applications.

Recent Comments

Dave on CakePHP Auth Component - Users, Groups & Permissions Revisited
I dont understand how you define permissions for each contoller to User. Do I have to manually...
SI on CakePHP Auth Component - Users, Groups & Permissions Revisited
Hi Peter,

I am having little issue logging into the application. I have 2 user groups both having...
Alig on CakePHP Auth Component - Users, Groups & Permissions Revisited
Thank you very much for this tutorial. After fighting for hours to get the cakephp ACL working,...
Benny on CakePHP Auth Component - Users, Groups & Permissions Revisited
thank you for your solution it's perfect , you are genius , thank you this is the best ( beside...
Gordon on CakePHP Auth Component - Users, Groups & Permissions Revisited
I'm having a similar problem to luis. I followed everything as instructed here up to the point...