We use an example to describe how to customize the code template. Let's say we want to customize the code generated by the Model builder.
Let's start by creating a directory named Protected/gii/model/templates/customer. The model here means that we are going to override the default model generator. Templates/customer means that we will add a new code template set named Customer. Copy files framework/gii/generators/model/templates/default/model.php to Protected/gii/model/templates/customer. Now it's time to do some real work. Open the file protected/gii/model/templates/customer/model.php to edit it. Remember that this file will be used as a view file like this, meaning it can contain PHP expressions and statements. Let's change the template so that the generated code in the Attributelabels () method uses Yii::t () to translate the property label:
- Public Function Attributelabels ()
- {
- return Array (
- <?php foreach ($labels as $name = $label):?>
- <?php echo "' $name ' = yii::t (' Application ', ' $label '), \ n";?>
- <?php Endforeach;?>
- );
- }
In each code template, we can access some of the predefined variables, such as those in the example above $labels
. These variables are provided by the corresponding code generator. Different code generators may provide different variables in their code templates. Please read the description in the default code template carefully.
Now open the Model Code generator page. Click the Code Template input box. We should see a drop-down list that contains our new template catalog customer. We select this template to generate a code file.
The framework generates a controller with the following template: framework/gii/generators/controller/templates/default/controller.php
The template for the framework generation model is: framework/gii/generators/model/templates/default/model.php
Advanced ) Create a new generator
To create a widget folder in Framework/gii/generators, you can write a crud Generator that supports module,ModuleID the next model The writing of class Application.modules.moduleID.models.modelClass
You can put your expanded GII below the project directory, configured as follows
- ' Modules ' = Array (
- ' gii ' = Array (
- ' class ' = ' system.gii.GiiModule ',
- ' password ' = ' gii ',
- ' generatorpaths ' = Array (
- ' application.gii.generators ',//project directory structure
- ),
- ' ipfilters ' = = Array (' 127.0.0.1 ', ':: 1 '),
- ),
- ),
The above configuration tells the GII that it is also possible to look for generators in the directory where aliases are application.gii.generators, as well as the default framework locations System.gii.generators generators with the same name in different search paths. In this case, the generator that appears first in the giimodule::generatorpaths specified directory has precedence.
YII2 customizing GII Generating code templates