How to create a breadcrumb widget

Here's a simple way to create a breadcrumb widget to be used in your templates. The idea is just to isolate how the breadcrumb is generated based on an array of crumbs.

>Info: Please note, that new versions of Yii Framework already contain a bulit-in breadcrumbs widget. See this class reference chapter for more information. Information in this article may be, however, usable to build custom one, not descending from bulit-in one.


class BreadCrumb extends CWidget {
	public $crumbs = array();
	public $delimiter = ' / ';
	public function run() {



<div id="breadCrumb">	
	foreach($this->crumbs as $crumb) {
		if(isset($crumb['url'])) {
			echo CHtml::link($crumb['name'], $crumb['url']);
		} else {
			echo $crumb['name'];
		if(next($this->crumbs)) {
			echo $this->delimiter;

Usage in views ΒΆ

<?php $this->widget('application.components.BreadCrumb', array(
  'crumbs' => array(
    array('name' => 'Home', 'url' => array('site/index')),
    array('name' => 'Login'),
  'delimiter' => ' &rarr; ', // if you want to change it
)); ?>
10 0
Viewed: 80 298 times
Version: 1.1
Category: Tutorials
Written by: knut
Last updated by: Yang He
Created on: Mar 19, 2009
Last updated: 8 years ago
Update Article


View all history