Yii2 Admin module
, (*1)
Introduction
Yii2AdminModule -- Module for the Yii2 framework with AdminLTE template, which provides the following options:
- Use this module as base administrator dashboard to manage site content with the ability to extend it by children application CRUD's
- Work in multilanguage mode for content
- Work in multilanguage mode for dashboard, (*2)
Module functional block scheme:, (*3)
, (*4)
Note!!!, (*5)
As this module gives you a freedom in the architecture of your application child classes, pay attention:
- There are no the next specific entity model classes, which are as examples in documentation: Catalog
, CatalogLanguage
- you have to create them personally in your application.
- There are no the next specific controller class, which is as example in documentation: CatalogController
- you have to create it personally in your application.
- There are not routes - you have to create that according with your application controller map., (*6)
Base project example, which uses this admin module for multilanguage content and dashboard: yii2-template-multilanguage., (*7)
Base project example, which uses this admin module for simple data: yii2-template-simple., (*8)
Addition module description you can see in my Personal site., (*9)
Dependencies
Installation
Via composer:, (*10)
composer require itstructure/yii2-admin-module ~1.8.7
, (*11)
Usage
Main properties
The name of module: admin
, (*12)
The namespace for used classes: Itstructure\AdminModule
., (*13)
The alias to access in to module root directory: @admin
., (*14)
The main module template layout is in: @admin/views/layouts/main-admin.php
., (*15)
Application config
Base application config must be like in example below:, (*16)
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',
],
],
],
],
Here,, (*17)
viewPath
- view template location for CRUDs, which you will create in application., (*18)
CatalogController
- example controller., (*19)
accessRoles
- an array of roles, that are allowed to access. Bu default ['@']
., (*20)
in components
the view
component contains required parameter class
.
Such parameters as skin
, bodyLayout
- dashboard style, can be set custom., (*21)
Parameter mainMenuConfig
- is the sidebar admin menu, which must be an array:, (*22)
[
'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',
],
],
]
Parameter extraAssets
- extra assets, can be like in examples:, (*23)
-
Like a class names, (*24)
[
FirstExtraAsset::class,
SecondExtraAsset::class,
]
-
Or like an array config, (*25)
[
[
'class' => FirstExtraAsset::class,
],
[
'class' => SecondExtraAsset::class,
],
]
Base controllers
In Yii2AdminModule there are two controllers, which can be used by child application
controllers:, (*26)
-
AdminController
, (*27)
When using this controller, a class AdminView will be loaded with its assets. This is done
using getView() method, which in turn is taken from module class with getting the
view component., (*28)
-
CommonAdminController
, (*29)
This controller extends AdminController
.
There are already created universal basic methods for child application controllers:, (*30)
-
actionIndex()
;
-
actionView($id)
;
-
actionCreate()
;
-
actionUpdate($id)
;
-
actionDelete($id)
;
In order for the child application controller to work with the CommonAdminController, it is
necessary to define the functions in child controller:, (*31)
-
getModelName()
; Sets the name of main model class.
-
getSearchModelName()
; - Sets the name of main search model class.
In CommonAdminController there are the next interesting options:, (*32)
-
viewCreated
- to view the record after it's creation instead all record list.
-
additionFields
- array of addition fields with heir values for the view template.
-
additionAttributes
- array of addition attributes with their values for current
model, which can be set in model except for those that are sent from the form.
Example: information about the uploaded file, which was uploaded separate from the main send from the form.
Integrated controllers
In Yii2AdminModule there is one integrated controller:, (*33)
-
LanguageController
- to manage just languages for application data.
Multilanguage mode just for dashboard you can set by language
parameter in app configuration: en-US
, ru-RU
e.t.c., (*34)
Multilanguage mode for data
There is an opportunity to set modes by application configuration, using parameter:, (*35)
-
isMultilanguage
- work in multilanguage mode just for content., (*36)
When true, the sidebar link "Languages" in main menu will appear automatically., (*37)
And application configuration will take an appearance:, (*38)
use Itstructure\AdminModule\Module;
use Itstructure\AdminModule\components\AdminView;
'modules' => [
'admin' => [
'class' => Module::class,
'viewPath' => '@app/views/admin',
'controllerMap' => [
'catalog' => CatalogController::class,
],
...
...
'isMultilanguage' => true,
],
],
In multilanguage mode, such a standard is adopted:, (*39)
-
All multilanguage fields will be with a language postfix:, (*40)
title_en, (*41)
description_en, e t. c., (*42)
-
Single and multilanguage fields will be rendered in view template by spectial fields widget., (*43)
-
All fields values after form sending will be loaded to the
MultilanguageValidateModel by MultilanguageValidateComponent in CommonAdminController., (*44)
-
Then MultilanguageValidateModel will load validated values in to the main model., (*45)
-
In main model the field values will be processed by MultilanguageTrait to separate
their between own main model and translate model., (*46)
-
As a model in view template, it will the MultilanguageValidateModel, that is set by
MultilanguageValidateComponent in CommonAdminController., (*47)
-
Rules configuration for multilanguage fields validation need to be set in individual component
config., (*48)
-
Rules configuration for single fields validation need to be set in the main model., (*49)
-
All the above configurations will be combined in MultilanguageValidateModel for general
process., (*50)
To use this mode it's necessary:, (*51)
-
Set module parameter isMultilanguage
on true in application config section mdules -> admin., (*52)
-
Apply module migration: migrations/multilanguage/m171202_104405_create_language_table
, (*53)
For that make next, if not already done:, (*54)
-
Define admin module in application console config file:, (*55)
use Itstructure\AdminModule\Module;
'modules' => [
'admin' => [
'class' => Module::class,
],
],
-
In file yii, that is located in application root directory, set @admin alias:, (*56)
$application = new yii\console\Application($config);
\Yii::setAlias('@admin', $application->getModule('admin')->getBasePath());
$exitCode = $application->run();
OR, (*57)
use Itstructure\AdminModule\Module;
$application = new yii\console\Application($config);
\Yii::setAlias('@admin', Module::getBaseDir());
$exitCode = $application->run();
-
Run command in console:, (*58)
yii migrate --migrationPath=@admin/migrations/multilanguage
, (*59)
-
Check if the sidebar link to manage languages is appeared., (*60)
-
Application migrations must be extended from
Itstructure\AdminModule\components\MultilanguageMigration
, (*61)
It's necessary to automatically creation main table and translate table., (*62)
-
Data base tables will have a structure, like in example:, (*63)
Main table "catalog"
, (*64)
| 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 |
Translate table "catalog_language"
, (*65)
| 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 table "language"
, (*66)
| 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 |
Here,, (*67)
Not multilanguage fields: id, order., (*68)
Multilanguage fields: title, description., (*69)
-
For all described tables above, it is necessary to create CRUD model classes, like in exemple:, (*70)
Catalog - main model, (*71)
CatalogLanguage - translate model, (*72)
Language - already exists in module!, (*73)
-
In child application admin controller define function getModelName(), like in example:, (*74)
protected function getModelName():string
{
return Catalog::class;
}
-
In child application admin controller define function getSearchModelName(), like in example:, (*75)
protected function getSearchModelName():string
{
return CatalogSearch::class;
}
-
In main model, use trait, like in example:, (*76)
use Itstructure\AdminModule\models\MultilanguageTrait;
class Catalog extends ActiveRecord
{
use MultilanguageTrait;
...
Using this trait, the multilanguage fields values, that are set like for example
title_en, description_en, will be redirected to the translate model automatically., (*77)
Not multilanguage fields, such as order e. t. c., will be set in to the main model
automatically., (*78)
-
To display form fields in view templates use special widget (installed by composer.json dependency):, (*79)
use Itstructure\FieldWidgets\Fields.php;
as in example:, (*80)
$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();
Here,, (*81)
-
Language - is a class in Itstructure\AdminModule\models\Language.php
, (*82)
-
This widget (when for example two languages ru and en) will parse the form fields so:, (*83)
title_en, (*84)
title_ru, (*85)
description_en, (*86)
description_ru, (*87)
-
if do not set the attribute languageModel, form fields will not be multilanguage and
will be single., (*88)
-
$model - will be set automatically in CommonAdminController as object of
Itstructure\AdminModule\models\MultilanguageValidateModel
, in which the main model can
be set after define her class name by method getModelName()., (*89)
-
Configure the multilanguage component multilanguage-validate-component
for admin module with rules for validation
multilanguage data in application config, like in example:, (*90)
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',
]
]
],
],
]
]
]
],
]
]
Useful feature:, (*91)
After release 1.6.2 in MultilanguageValidateModel
class the mainModelAttributes() method checking is added to check its presence in main model.
This may be necessary when you need to validate fields that are not in the database table. These fields can be set in this method, in addition to the main fields. The method must return an array., (*92)
License
Copyright © 2018-2023 Andrey Girnik girnikandrey@gmail.com., (*93)
Licensed under the MIT license. See LICENSE.txt for details., (*94)