Skip to content

itnelo/yii2-breadcrumbs-filter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Yii2 Breadcrumbs ActionFilter

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 itnelo/yii2-breadcrumbs-filter:~1.0

or add

"itnelo/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' => \itnelo\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 itnelo\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.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages