Download the PHP package mgrechanik/yii2-universal-module-sceleton without Composer
On this page you can find all versions of the php package mgrechanik/yii2-universal-module-sceleton. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mgrechanik/yii2-universal-module-sceleton
More information about mgrechanik/yii2-universal-module-sceleton
Files in mgrechanik/yii2-universal-module-sceleton
Package yii2-universal-module-sceleton
Short Description The structure of universal Yii2 module who fits both to advanced and basic templates
License BSD-3-Clause
Informations about the package yii2-universal-module-sceleton
Yii2 universal module sceleton
Русская версия
Table of contents
- Goal
- Installing
- What it is about
- Using
- Module inheritance
- Module settings
- Example with Basic template
- How-to
Goal
This extension gives the structure of the module which:
- will be self-sufficient and portable because it holds all his functionality in one place
- logically divided at backend and frontend parts
- easily connects both to Advanced and Basic application templates
-
with Basic application template:
- connects to module section only one time
- there is functionality of protection to all backend controllers (for admin part of your web site)
- it is easy to inherit such modules from one another to override functionality without controllers duplication
Installing
The preferred way to install this extension is through composer.:
Either run
or add
to the require section of your composer.json
What it is about
- By default module controllers are being searched automatically in it's
- We do not use this functionality but define all our controllers in module's
- But we do this not by property explicitly but define backend and frontend controllers separately
- Module has the mode which is set in config; according to this mode
will have only those controllers who fit the mode:
- With frontend application of Advanced template we connect our module in mode
- With backend application of Advanced template we connect our module in mode
- With Basic template we can connect our module in two modes described above and also in mode when both controller types are accessible
- When module get the request it creates the controller from their map, figuring by it's namespace
whether it is backend or frontend controller to perform additional set up - Module expects the next directory structure:
Using
1) Generate, or create manually, your module class
2) Inherit your module class from universal module class
3) Now create (or generate) frontend controller
- Take into consideration that it's should be
- Create all subdirs needed
- According to controller it's views will reside in
- We need to define this controller in frontend controller map of this module:
, where match .
When the name and class of controller do not match use next definition form: .
Always when you create new controller do not forget to define it in appropriate controller map of your module.
You are not required to inherit your controller classes from any parent type.
4) Now create (or generate) backend controller
- Logic is the same with 3), but it's should be
-
Define it in module at:
- It is handy to prefix backend controller names with Admin, so all backend urls could be set up the way all of them will start with admin/
5) Done, your module is ready, you can connect it to application:
config/main.php:
, do not forget to define - mode
It is comfortable to connect all such modules at first level of application modules, without nested modules but like a simple list of modules we used to see at admin pages of popular CMSs, which also gives short urls.
Module inheritance
When you inherit your module class from existing one without overriding and the next happens: 1) These two properties will be taken from parent naturally 2) But the basic controller namespace will be set to your module namespace. You do not have such controllers and you do not want to create their copies 3) There are next opportunities how to make your module to use controllers from any of his ancestors:
-
If controllers reside with immediate parent of your module set it the next property
- If controllers are in some other module, say , then set the property of your module to namespace of module
- will be searched by default according to settings above
Module settings
Connecting module to application we can use next it's properties:
- mode in which this module works
You are required to set up it. Details
- layout for backend controllers
Sets up to module when backend controller is requested.
It is useful for Basic application template.
- frontend controller map
Details
- backend controller map
Details
- callback for final adjustment of controller map
After module's controller map is generated you can adjust it with this function which signature is:
- backend controllers settings
When module creates backend controller it could set up controller with these properties.
It is handy, for example, to restrict access to such controllers using yii filters connected like behaviors.
Example of using.
- frontend controllers settings
It is the same like
- the basic namespace for the controllers the module uses
By default it is not used and controllers will be searched relevant to your current module namespace.
But with this property you can tell to take controllers from any other module
- whether to take controllers from parent
It is by default, meaning no taking.
But with this property you can tell to take controllers from immediate parent of current module
- basic path where to find module's
By default it is not used.
It is automatically set up whether to current module directory or relevant to the two properties above when they are set.
But with this property you can finally say where to search for . Example of value:
Example of module's set up with Basic application template
Lets suppose that we have two modules we are talking about - and .
Here is working configs to set up these modules:
config/params.php:
At this config we gave permission to "admin pages" only to one user , with additional check for .
config/web.php:
How-to
Make all admin urls start with
Lets see Basic application template with two "our" modules connected to it:
If we followed advice above about naming of backend controllers all of them have names like .
So urls to them will be and .
And we want all our admin urls to start with .
It is easily achived with the next two for your :
Generating backend functionality with Gii CRUD generator
You can easily generate CRUD functionality considering that:
- The and the of the controller should be choosen according to documentation
- should match directory structure the module demands
How to connect the module to console application?
If our module has console commands who reside for example here:
, then in the console application config this module is connected like:
Where to put all other module's functionality?
This module regulates only directory structure described above where only
from controllers and views their concrete positions are expected.
When writing the rest of functionality you may follow the next advices:
- If a component is definitely related only to one part of application - backend or frontend then put it in the corresponding subdirectory
- If there is no such definite separation put it in the root of his directory
For example for models:
- Since for all user interface of our module we have already created subdirectory then put forms and widgets there