itstructure / yii2-admin-module
基于 AdminLTE 模板的 Yii2 框架管理模块
Requires
- php: >=7.1.0
- almasaeed2010/adminlte: ~2.4.3
- itstructure/yii2-field-widgets: ^1.2.0
- yiisoft/yii2: 2.*
- yiisoft/yii2-bootstrap: ^2.0
README
简介
Yii2AdminModule -- 基于AdminLTE模板的Yii2框架模块,提供以下选项
- 将此模块用作基础管理员仪表盘,用于管理网站内容,并可以通过子应用CRUD扩展它
- 内容多语言模式
- 仪表盘多语言模式
模块功能模块方案
注意!!!
由于此模块在应用程序子类架构方面给您提供了自由度,请注意
- 没有以下特定实体模型类,如文档中的示例:
Catalog,CatalogLanguage- 您必须亲自在您的应用程序中创建它们。 - 没有以下特定控制器类,如文档中的示例:
CatalogController- 您必须亲自在您的应用程序中创建它。 - 没有路由 - 您必须根据您的应用程序控制器映射创建它们。
使用此管理模块用于多语言内容和仪表盘的基本项目示例: yii2-template-multilanguage。
使用此管理模块用于简单数据的基准项目示例: yii2-template-simple。
更多模块描述,请参阅我的 个人网站。
依赖关系
- php >= 7.1
- composer
安装
通过 composer
composer require itstructure/yii2-admin-module ~1.8.7
用法
主要属性
模块的 名称: admin
使用类的 命名空间: Itstructure\AdminModule。
访问模块根目录的 别名: @admin。
主要模块模板 布局 在: @admin/views/layouts/main-admin.php。
应用程序配置
基本应用程序配置必须如下所示
use Itstructure\AdminModule\Module; use Itstructure\AdminModule\components\AdminView;
'modules' => [ 'admin' => [ 'class' => Module::class, 'viewPath' => '@app/views/admin', 'controllerMap' => [ 'catalog' => CatalogController::class, ], 'accessRoles' => ['admin', 'manager'], 'components' => [ 'view' => [ 'class' => AdminView::class, 'skin' => AdminView::SKIN_GREEN_LIGHT, 'bodyLayout' => AdminView::LAYOUT_SIDEBAR_MINI, 'mainMenuConfig' => require __DIR__ . '/main-menu.php' 'extraAssets' => require __DIR__ . '/extra-assets.php', ], ], ], ],
这里,
viewPath - CRUD 视图模板位置,您将在应用程序中创建。
CatalogController - 示例控制器。
accessRoles - 允许访问的角色数组。默认为 ['@']。
在 components 中,view 组件包含所需参数 class。例如,skin,bodyLayout - 仪表盘样式,可以自定义设置。
mainMenuConfig 参数 - 是侧边栏管理员菜单,必须是一个数组
[
'menuItems' => [
'catalog' => [
'title' => 'Catalog',
'icon' => 'fa fa-database',
'url' => '#',
'subItems' => [
'subitem' => [
'title' => 'Subcatalog',
'icon' => 'fa fa-link',
'url' => '/admin/catalog',
]
]
],
'otheritem' => [
'title' => 'Other Item',
'icon' => 'fa fa-database',
'url' => '/admin/otheritem',
],
],
]
extraAssets 参数 - 额外资源,可以像以下示例一样
-
类名称
[ FirstExtraAsset::class, SecondExtraAsset::class, ] -
或如数组配置
[ [ 'class' => FirstExtraAsset::class, ], [ 'class' => SecondExtraAsset::class, ], ]
基本控制器
在 Yii2AdminModule 中,有两个控制器,可以被子应用控制器使用
-
AdminController使用此控制器时,将加载一个带有其资源的 AdminView 类。这是通过 getView() 方法完成的,该方法反过来从 模块 类中获取 view 组件。
-
CommonAdminController此控制器扩展了
AdminController。已为子应用控制器创建了通用的基本方法actionIndex();actionView($id);actionCreate();actionUpdate($id);actionDelete($id);
为了让子应用程序控制器与CommonAdminController协同工作,需要在子控制器中定义函数
getModelName();设置主模型类的名称。getSearchModelName();- 设置主搜索模型类的名称。
在CommonAdminController中有以下有趣的功能
viewCreated- 在创建记录后查看记录,而不是所有记录列表。additionFields- 用于视图模板的附加字段及其值数组。additionAttributes- 附加属性及其值的数组,用于当前模型,可以在模型中设置,除了从表单发送的属性。例如:关于单独上传的文件的信息,该文件是从主要发送的表单中单独上传的。
集成控制器
在Yii2AdminModule中有一个集成控制器
LanguageController- 用于管理应用程序数据中的语言。
仪表板工具的多语言模式
仪表板的多语言模式可以通过在应用程序配置中的language参数中设置来设置:en-US、ru-RU等。
数据的多语言模式
可以通过应用程序配置中的参数设置模式
-
isMultilanguage- 仅在内容中工作在多语言模式。当为true时,主菜单中的“语言”侧边栏链接将自动出现。
并且应用程序配置将采取以下外观
use Itstructure\AdminModule\Module; use Itstructure\AdminModule\components\AdminView;
'modules' => [ 'admin' => [ 'class' => Module::class, 'viewPath' => '@app/views/admin', 'controllerMap' => [ 'catalog' => CatalogController::class, ], ... ... 'isMultilanguage' => true, ], ],
在多语言模式下,采用以下标准
-
所有多语言字段都将带有语言后缀
title_en
description_en等。
-
单语言和多语言字段将由特殊的字段小部件在视图模板中渲染。
-
在表单提交后,所有字段值将通过CommonAdminController中的MultilanguageValidateComponent加载到MultilanguageValidateModel。
-
然后,MultilanguageValidateModel将加载验证值到主模型。
-
在主模型中,字段值将通过MultilanguageTrait进行处理,以区分它们之间的主模型和翻译模型。
-
在视图模板中的模型将是MultilanguageValidateModel,这是CommonAdminController中的MultilanguageValidateComponent设置的。
-
对于多语言字段验证的规则配置需要设置在单个组件配置中。
-
对于单字段验证的规则配置需要设置在主模型中。
-
所有上述配置将在MultilanguageValidateModel中组合,以进行一般处理。
要使用此模式,需要
-
在应用程序配置的
modules部分将模块参数isMultilanguage设置为true->admin。 -
应用模块迁移:
migrations/multilanguage/m171202_104405_create_language_table为此,请执行以下操作,如果尚未完成
-
在应用程序控制台配置文件中定义
admin moduleuse Itstructure\AdminModule\Module;
'modules' => [ 'admin' => [ 'class' => Module::class, ], ],
-
在应用程序根目录中位于文件
yii中,设置@admin别名$application = new yii\console\Application($config); \Yii::setAlias('@admin', $application->getModule('admin')->getBasePath()); $exitCode = $application->run();
或者
use Itstructure\AdminModule\Module;
$application = new yii\console\Application($config); \Yii::setAlias('@admin', Module::getBaseDir()); $exitCode = $application->run();
-
在控制台中运行命令
yii migrate --migrationPath=@admin/migrations/multilanguage -
检查管理语言链接是否出现。
-
-
应用程序迁移必须从
Itstructure\AdminModule\components\MultilanguageMigration扩展。需要自动创建主表和翻译表。
-
数据库表的结构如下所示
主表 "catalog"| id | order | created_at | updated_at | |----|-------|---------------------|---------------------| | 1 | 2 | 2018-01-14 18:06:33 | 2018-01-14 18:06:33 | | 2 | 1 | 2018-01-14 18:10:00 | 2018-01-14 18:10:00 | | 3 | 3 | 2018-01-14 19:05:15 | 2018-01-14 19:05:15 |翻译表 "catalog_language"| catalog_id | language_id | title | description | created_at | updated_at | |------------|-------------|-----------|----------------------|---------------------|---------------------| | 1 | 1 | Catalog 1 | Description 1 | 2018-01-14 18:06:33 | 2018-01-14 18:06:33 | | 1 | 2 | Каталог 1 | Описание 1 | 2018-01-14 18:06:33 | 2018-01-14 18:06:33 | | 2 | 1 | Catalog 2 | Description 2 | 2018-01-14 18:10:00 | 2018-01-14 18:10:00 | | 3 | 1 | Catalog 3 | Description 3 | 2018-01-14 19:05:15 | 2018-01-14 19:05:15 | | 3 | 2 | Каталог 3 | Описание 3 | 2018-01-14 19:05:15 | 2018-01-14 19:05:15 |语言表 "language"| id | locale | shortName | name | default | created_at | updated_at | |----|--------|-----------|---------|---------|---------------------|---------------------| | 1 | en-US | en | English | 1 | 2018-01-14 18:06:33 | 2018-01-14 18:06:33 | | 2 | ru-RU | ru | Русский | 0 | 2018-01-14 18:10:00 | 2018-01-14 18:10:00 |这里,
非多语言字段:id,order。
多语言字段:title,description。
-
对于上述所有描述的表,需要创建CRUD模型类,例如
Catalog - 主模型
CatalogLanguage - 翻译模型
Language - 已在模块中存在!
-
在子应用程序的admin控制器中定义getModelName()函数,如下所示
protected function getModelName():string { return Catalog::class; }
-
在子应用程序的admin控制器中定义getSearchModelName()函数,如下所示
protected function getSearchModelName():string { return CatalogSearch::class; }
-
在主模型中使用特性,如下所示
use Itstructure\AdminModule\models\MultilanguageTrait; class Catalog extends ActiveRecord { use MultilanguageTrait; ...
使用此特性,设置如 title_en、description_en 等的多语言字段值将被自动重定向到翻译模型。
非多语言字段,例如 order 等,将被自动设置到主模型中。
-
要在视图模板中显示表单字段,请使用特殊的小部件(由composer.json依赖项安装)
use Itstructure\FieldWidgets\Fields.php;
如下所示
$form = ActiveForm::begin();
echo Fields::widget([ 'fields' => [ [ 'name' => 'title', 'type' => FieldType::FIELD_TYPE_TEXT, ], [ 'name' => 'description', 'type' => FieldType::FIELD_TYPE_TEXT_AREA, ], ], 'model' => $model, 'form' => $form, 'languageModel' => new Language() ])
Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary'])
ActiveForm::end();
这里,
-
语言 是位于
Itstructure\AdminModule\models\Language.php的类 -
此小部件(例如,对于两种语言 ru 和 en)将解析表单字段,如下所示
title_en
title_ru
description_en
description_ru
-
如果没有设置 languageModel 属性,则表单字段将不是多语言的,将是单语言的。
-
$model 将在 CommonAdminController 中自动设置为
Itstructure\AdminModule\models\MultilanguageValidateModel的对象,其中主模型可以在通过方法 getModelName() 定义其类名后设置。
-
-
在应用程序配置中配置多语言组件
multilanguage-validate-component以 admin 模块的验证多语言数据规则,如下所示use Itstructure\AdminModule\Module; use Itstructure\AdminModule\components\AdminView; use Itstructure\AdminModule\components\MultilanguageValidateComponent;
'modules' => [ 'admin' => [ 'class' => Module::class, 'viewPath' => '@app/views/admin', 'controllerMap' => [ 'catalog' => CatalogController::class, ... ], 'isMultilanguage' => true, 'components' => [ 'view' => [ 'class' => AdminView::class, 'skin' => AdminView::SKIN_GREEN_LIGHT, 'bodyLayout' => AdminView::LAYOUT_SIDEBAR_MINI, 'mainMenuConfig' => require __DIR__ . '/main-menu.php' ], 'multilanguage-validate-component' => [ /** * Component class. */ 'class' => MultilanguageValidateComponent::class, /** * List of models. * Each model is identified by the name of the table. * In the config attributes of each model, you need to specify: * Dynamic (translated fields) dynamicFields. * Field dynamicFields needs to have: 'name' - field name. * Field dynamicFields (not necessary) may have 'rules'. */ 'models' => [ Catalog::tableName() => [ 'dynamicFields' => [ [ 'name' => 'title', 'rules' => [ [ 'required', 'message' => 'Field "{attribute}" must not be empty.' ], [ 'string', 'max' => 255, ], [ 'unique', ] ] ], [ 'name' => 'description', 'rules' => [ [ 'required', 'message' => 'Field "{attribute}" must not be empty.' ], [ 'string', ] ] ], ], ] ] ] ], ] ]
有用功能
从版本1.6.2开始,在 MultilanguageValidateModel 类中添加了检查 mainModelAttributes() 方法以检查其在主模型中的存在。这可能在您需要验证不在数据库表中的字段时是必要的。除了主字段外,还可以在此方法中设置这些字段。此方法必须返回一个数组。
许可证
版权所有 © 2018-2023 Andrey Girnik girnikandrey@gmail.com。
根据 MIT 许可证 授予许可。有关详细信息,请参阅 LICENSE.txt。
