yii2-tools/yii2-breadcrumbs-filter

Filter for the Yii2 Framework which automatically append module as breadcrumb item

1.0.8 2016-04-09 11:27 UTC

This package is auto-updated.

Last update: 2022-05-09 23:21:21 UTC


README

Latest Version Software License Quality Score Total Downloads

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.