yii2-tools / yii2-breadcrumbs-filter
Filter for the Yii2 Framework which automatically append module as breadcrumb item
Installs: 300
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 1
Type:yii2-extension
Requires
- yiisoft/yii2: ~2.0.0
This package is auto-updated.
Last update: 2024-12-10 05:22:11 UTC
README
Yii2 ActionFilter which automatically append module as breadcrumb item if his id exists in requested route.
Installation
The preferred way to install this extension is through composer.
Either run
$ composer require yii2-tools/yii2-breadcrumbs-filter:~1.0
or add
"yii2-tools/yii2-breadcrumbs-filter": "~1.0"
to the require
section of your composer.json
file.
Usage
Attach behavior to module:
public function behaviors() { return array_merge(parent::behaviors(), [ 'breadcrumbs' => [ 'class' => \yii\tools\filters\BreadcrumbsFilter::className(), ] ]); }
In view file (perhaps, layout):
<div class="container"> <?= \yii\widgets\Breadcrumbs::widget([ 'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [], ]) ?> </div>
Best practices
You can unify building of site breadcrumbs navigation by extending yii\base\Module. It will guarantee what all modules in requested route gets their place in breadcrumbs widget. Example:
use yii\base\Module as BaseModule; use yii\tools\filters\BreadcrumbsFilter; class Module extends BaseModule { /** * Module name * @var string */ public $name = 'My Module'; /** * Enable/Disable breadcrumbs natigation via app\components\filters\BreadcrumbsFilter * For module and submodules, without affects on parent module * @var bool */ public $breadcrumbs = true; /** * Array of [routes|controllers|actions] names which shouldn't have breadcrumbs * ['*'] means what breadcrumbs navigation disabled for all controllers and actions (direct childs) * For module and submodules, without affects on parent module * @var bool */ public $breadcrumbsExceptRoutes = []; /** * @inheritdoc */ public function behaviors() { $behaviors = []; if ($this->breadcrumbs) { $behaviors['breadcrumbs'] = [ 'class' => BreadcrumbsFilter::className(), 'label' => $this->name, 'defaultRoute' => $this->defaultRoute, 'exceptRoutes' => $this->breadcrumbsExceptRoutes, ]; } return array_merge(parent::behaviors(), $behaviors); } }
License
The MIT License (MIT). Please see License File for more information.