mix8872/yii2-menu 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mix8872/yii2-menu

Composer 安装命令:

composer require mix8872/yii2-menu

包简介

Yii2 menu module

README 文档

README

Module for create menu on you website.

Installation

The preferred way to install this extension is through composer.

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

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

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 menu item is the ordinary item with url and title. That's all.

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:

<?= \mix8872\menu\widgets\MenuWidget::widget([
        'code' => 'Tx1rNy'
    ])
?>

But widget can be configured in more detail. Next options is available:

/**
 * Append class to the ul element
 * @var string
 */
public $menuClassName = '';

/**
 * Append id to the 'ul' element
 * @var string
 */
public $menuId = '';

/**
 * Append class to the all 'li' elements which are not parents or descendants
 * @var string
 */
public $itemsClassName = '';

/**
 * Append class to the all 'a' elements in the parent elements
 * @var string
 */
public $linkClassName = '';

/**
 * Append class to the all 'a' elements in the descendant elements
 * @var string
 */
public $sublinkClassName = '';

/**
 * Append class to the all descendant 'ul' elements
 * @var string
 */
public $submenuClassName = '';

/**
 * Append class to the all descendant 'li' elements
 * @var string
 */
public $subitemsClassName = '';

/**
 * Show description
 * @var bool
 */
public $description = false;

/**
 * Menu code
 * @var
 */
public $code;

/**
 * Active 'li' element class name
 * @var string
 */
public $activeClassName = 'active';

/**
 * Append class to the all parent 'li' elements
 * @var string
 */
public $parentClassName = '';

/**
 * Menu type: classic (default) or buttons
 * @var string
 */
public $type = 'classic';

/**
 * Tag to wrap submenu 'ul' element
 * @var
 */
public $submenuWrapperTag;

/**
 * Append class to submenu wrapper (submenuWrapperTag must defined)
 * @var string
 */
public $submenuWrapperClassName = '';

/**
 * If false (default) then parents elements has no links and shown as 'span'
 * @var bool
 */
public $isParentActive = false;

/**
 * Template for item 'a' elements which are not parents
 * @var string
 */
public $itemTemplate = '{link}';

/**
 * Template for item 'a' elements which are parents
 * @var string
 */
public $parentTemplate = '{link}';

/**
 * Template for all item 'a' elements
 * @var string
 */
public $linkTemplate = '{link}';

/**
 * Template title of all item 'a' elements
 * @var string
 */
public $linkTitleTemplate = '{url}';

/**
 * If true then current url will be excluded from menu, default false
 * @var bool
 */
public $exceptCurrent = false;

/**
 * Array of url's which must excluded from menu
 * @var array
 */
public $exceptItems = [];

Events

On adding, updating and deletion files will generates next events:

  • EVENT_AFTER_CREATE = 'menuAfterCreate' - fires after creation new menu
  • EVENT_AFTER_DELETE = 'menuAfterDelete' - fires after deletion menu
  • EVENT_AFTER_DELETE_ITEM = 'menuAfterDeleteItem' - fires after deletion menu item
  • EVENT_AFTER_ADD_ITEM = 'menuAfterAddItem' - fires after add menu item
  • EVENT_AFTER_SORT = 'menuAfterSort' - fires after sort menu items
  • EVENT_AFTER_UPDATE = 'menuAfterUpdate' - fire after update menu,
    also fires when EVENT_AFTER_ADD_ITEM, EVENT_AFTER_DELETE_ITEM and EVENT_AFTER_SORT

You can intercept events in the module configuration as in the example above.

mix8872/yii2-menu 适用场景与选型建议

mix8872/yii2-menu 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 424 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 05 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 mix8872/yii2-menu 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mix8872/yii2-menu 我们能提供哪些服务?
定制开发 / 二次开发

基于 mix8872/yii2-menu 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 424
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-05-26