skylineos / yii-menu

用于创建和管理菜单的Yii2扩展

安装: 75

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 5

分支: 0

开放问题: 0

类型:yii2-extension

0.9.13 2022-11-08 12:51 UTC

This package is auto-updated.

Last update: 2024-09-08 16:57:16 UTC


README

安装

Composer

"skylineos/yii-menu": "~1.0"

运行迁移

php yii migrate/up --migrationPath=vendor/skylineos/yii-menu/src/migrations

虽然这不是强制性的,但建议你在自己的应用中创建迁移,将属性 createdBymodifiedBy 作为外键关联到你的User模型的主键(无论是什么)。

配置完成后(如下),你应该可以通过访问 /menu/menu/index 来使用管理器

配置

config/web.php

'modules' => [
    'menu' => [
        'class' => 'skylineos\yii\menu\Module',
        'viewPath' => '@app/path/to/my/admin/views', // eg. @app/modules/cms/views. The system looks for [menu|menu-item] folders
        'roles' => ['@'], // optional yii authorization roles
        'dropdownClass' => 'app\path\to\dropDownClass', // @see https://yiiframework.cn/extension/yiisoft/yii2-bootstrap4/doc/api/2.0/yii-bootstrap4-dropdown

        /**
         * Additional templates can be added as such: (namespace => display/friendly name)
         * @see [Building Templates]
         */
        'templates' => [
            'skylineos\yii\menu\widgets\SkyMenuWidget' => 'Useful for simple demo',
            'app\widgets\menus\OtherMenuWidget' => 'My Other Custom Widget',
        ],
        
        'targets' => [
            // See 'Defining Targets'
        ]
    ],
],

定义链接目标

目标(定义菜单项可以链接到的位置)可以通过模块的 'targets' 属性以三种方式处理

'targets' => [
    // Here, slug will be mapped to title, both pulled directly from Content
    'app\models\Content' => [
        'slug' => 'slug',
        'display' => 'title',
        'where' => [], // optional
        'orderBy' => 'title ASC', // optional
        'dropdownGroup' => 'Pages',
    ],

    // This will interpret the model property of Router and map the 'slug' to the 'model.title' based on the fk
    // literal => false
    'app\models\Router' => [
        'slug' => 'slug',
        'display' => [
            'literal' => false,
            'className' => 'model',
            'pk' => 'id', // The primary key on the foreign model. If not provided, 'id' will be assumed
            'fk' => 'modelId',
            'property' => 'title',
            'where' => [], // optional
            'orderBy' => 'title ASC' // optional
        ],
        'where' => [],
        'orderBy' => 'title ASC',
        'dropdownGroup' => 'All Routes',
    ],

    // This will load the literal map of ProductCatalog.slug to Product.metaTitle.
    // literal => true
    'app\models\ProductCatalog' => [
        'slug' => 'slug',
        'display' => [
            'literal' => true,
            'className' => 'app\models\Product',
            'pk' => 'id', // The primary key on the foreign model. If not provided, 'id' will be assumed
            'fk' => 'modelId',
            'property' => 'metaTitle',
        ],
        'where' => [],
        'orderBy' => 'Product.sortOrder ASC',
        'dropdownGroup' => 'Products',
    ],
],

使用方法

此包附带一个辅助小部件,它将处理你的菜单,并使用你在菜单项上配置的任何模板来渲染它。

确保你的模板(menuTemplatesdropDownTemplates)满足要求。请参阅构建模板

如果这些要求不符合你的需求,你当然可以开发自己的小部件,以你喜欢的任何方式渲染项目。

<?= \skylineos\yii\menu\widgets\SkyMenuWidget::widget(['menuId' => $menuId]) ?>

其中 $menuId 是你希望渲染的菜单的 id

构建模板

菜单模板

请参阅此存储库中的 examples/MenuTemplate.php

下拉模板

请参阅此存储库中的 examples/DropDownTemplate.php