PHPackages                             mix8872/yii2-menu - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. mix8872/yii2-menu

ActiveYii2-extension

mix8872/yii2-menu
=================

Yii2 menu module

1.1.2(4y ago)0424MITPHPPHP &gt;=7.1.0

Since May 26Pushed 4y ago1 watchersCompare

[ Source](https://github.com/mix8872/yii2-menu)[ Packagist](https://packagist.org/packages/mix8872/yii2-menu)[ RSS](/packages/mix8872-yii2-menu/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (5)Versions (6)Used By (0)

Yii2-menu module
================

[](#yii2-menu-module)

Module for create menu on you website.

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist mix8872/yii2-menu

```

or add

```
"mix8872/yii2-menu": "~1.0"

```

to the `require` section of your `composer.json`.

Then you must run migration by running command:

yii migrate --migrationPath=@vendor/mix8872/yii2-menu/src/migrations

Configure
---------

[](#configure)

To configure module please add following to the modules section of main config:

Backend:

```
'modules' => [
    'menu' => [
        'class' => 'mix8872\menu\Module',
        'on menuAfterCreate' => static function ($e) {
            $model = $e->model;
            // ... do something
        },
        'on menuAfterDelete' => static function ($e) {
            $model = $e->model;
            // ... do something
        },
        'on menuAfterUpdate' => static function ($e) {
            $model = $e->model;
            // ... do something
        },
        'on menuAfterSort' => static function ($e) {
            $model = $e->model;
            // ... do something
        },
        'on menuAfterAddItem' => static function ($e) {
            $model = $e->model;
            // ... do something
        },
        'on menuAfterDeleteItem' => static function ($e) {
            $model = $e->model;
            // ... do something
        },
    ],
    // ... other modules definition
],
```

Usage
-----

[](#usage)

After configuring go to the url `/menu/default/index` and press **Add** button,
fill the name, code and description of your new menu;

Now you can add menu items by pressing 'plus' green button in right side.
Menu items can be two types:

- simple
- from model

#### Simple

[](#simple)

Simple menu item is the ordinary item with url and title. That's all.

#### From model

[](#from-model)

That's 'magic' menu item which get menu items from ActiveRecord model
and replaces itself with the received data.

This menu item has next not ordinary fields:

- **Url** - base url, unlike a 'Simple', it contains **base** and **dynamic** parts of url
    the dynamic part must be enclosed in curly brackets and must be the same as **Url attribute**
    for example: `/news/{code}`, here `/news/` - is a basic part and `{code}` - is a dynamic part
    then **Url attribute** must declared as `code`
- **Model class** - here you should specify full model class name (of course with namespace)
- **Url attribute** - this is attribute name which will be used in the end part of url
- **Title attribute** - attribute from which the menu item title will be taken
    you can specify the name separated by a dot: `content.title`,
    then the value will be taken from the relative table, but you can set only one dot
- **Description attribute** - attribute from which the menu item description will be taken
    you can specify the name separated by a dot: `content.description`,
    then the value will be taken from the relative table, but you can set only one dot
- **Selection parameters** - there you can declare SQL-like parameters for selection of items from model
    for example: `WHERE status = 1 ORDER BY id DESC LIMIT 2`
    **!!! BE CAREFUL, IT IS EXPERIMENTAL FEATURE AND CAN BREAK YOUR APP !!!**

Next you may echo widget with its config.

The simplest case is:

```
