Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Yii2 Breadcrumbs

Tags:

yii2

I am looking for yii2 beadcrumb code

I have layout page in twig want to add breadcrumb there and want to update it according to pages for example

Dashboard/Employees/Add Employee

Dashboard/Employee/Edit Employee

Dashboard and employee will be clickable

I got very less information on yii2 breadcrumbs on internet.

like image 265
Junaid Ali Avatar asked Mar 09 '15 11:03

Junaid Ali


2 Answers

In your main layout use:

<?= 
   Breadcrumbs::widget([
      'homeLink' => [ 
                      'label' => Yii::t('yii', 'Dashboard'),
                      'url' => Yii::$app->homeUrl,
                 ],
      'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],
   ]) 
?>

And in your view:

$this->title = 'Add Employee';
$this->params['breadcrumbs'][] = ['label' => 'Employees', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;

This will generate the following breadcrumbs:

Dashboard / Employees / Add Employee
like image 81
Chinmay Waghmare Avatar answered Sep 25 '22 02:09

Chinmay Waghmare


In your layout-

//use yii\widgets\Breadcrumbs;

<?= 
   Breadcrumbs::widget([
        'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],
   ]) 
?>

In your view-

$this->title = 'Example';
$this->params['breadcrumbs'][] = $this->title;

Hope it will work.

like image 45
fool-dev Avatar answered Sep 22 '22 02:09

fool-dev