Yii2 framework backup
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
8.2 KiB

The Gii code generation tool
============================
Yii includes a handy tool, named Gii, that provides rapid prototyping by generating commonly used code snippets
as well as complete CRUD controllers.
11 years ago
Installing and configuring
--------------------------
Gii is an offical Yii extension. The preferred way to install this extension is through
[composer](http://getcomposer.org/download/).
11 years ago
You can either run this command:
11 years ago
```
php composer.phar require --prefer-dist yiisoft/yii2-gii "*"
11 years ago
```
Or you can add this code to the require section of your `composer.json` file:
11 years ago
```
"yiisoft/yii2-gii": "*"
```
Once the Gii extension has been installed, you enable it by adding these lines to your application configuration file:
```php
'modules' => [
'gii' => [
'class' => 'yii\gii\Module',
],
]
```
11 years ago
You can then access Gii through the following URL:
```
http://localhost/path/to/index.php?r=gii
```
> Note: if you are accessing gii from an IP address other than localhost, access will be denied by default. To circumvent that default, add the allowed IP addressess to the configuration:
11 years ago
>
```php
'gii' => [
'class' => 'yii\gii\Module',
'allowedIPs' => ['127.0.0.1', '::1', '192.168.0.*', '192.168.178.20'] // adjust this to your needs
],
```
### Basic application
In basic application template configuration structure is a bit different so Gii should be configured in
`config/web.php`:
```php
// ...
if (YII_ENV_DEV)
{
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = 'yii\debug\Module';
$config['modules']['gii'] = 'yii\gii\Module'; // <--- here
}
```
So in order to adjust IP address you need to do it like the following:
```php
if (YII_ENV_DEV)
{
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = 'yii\debug\Module';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
'allowedIPs' => ['127.0.0.1', '::1', '192.168.0.*', '192.168.178.20'],
];
}
```
11 years ago
How to use it
-------------
11 years ago
When you open Gii you first see the entry page that lets you choose a generator.
![Gii entry page](images/gii-entry.png)
By default there are the following generators available:
11 years ago
- **Model Generator** - This generator generates an ActiveRecord class for the specified database table.
- **CRUD Generator** - This generator generates a controller and views that implement CRUD (Create, Read, Update, Delete)
11 years ago
operations for the specified data model.
11 years ago
- **Controller Generator** - This generator helps you to quickly generate a new controller class, one or several
11 years ago
controller actions and their corresponding views.
11 years ago
- **Form Generator** - This generator generates a view script file that displays a form to collect input for the
11 years ago
specified model class.
11 years ago
- **Module Generator** - This generator helps you to generate the skeleton code needed by a Yii module.
- **Extension Generator** - This generator helps you to generate the files needed by a Yii extension.
11 years ago
After choosing a generator by clicking on the "Start" button you will see a form that allows you to configure the
parameters of the generator. Fill out the form according to your needs and press the "Preview" button to get a
preview of the code that gii is about to generated. Dependend on the generator you chose and whether the files
already existed or not you will get an ouput similar to what you see in the following picuture:
![Gii preview](images/gii-preview.png)
Clicking on the file name you can view a preview of the code that will be generated for that file.
When the file already exists, gii also provides a diff view that shows what is different between the code that exists
and the one that will be generated. In this case you can also choose which files should be overridden and which not.
11 years ago
> Tip: When using the Model Generator to update models after database change, you can copy the code from gii preview
and merge the changes with your own code. You can use IDE features like PHPStorms
[compare with clipboard](http://www.jetbrains.com/phpstorm/webhelp/comparing-files.html) for this,
which allows you to merge in relevant changes and leave out others that may revert your own code.
11 years ago
After you have reviewed the code and selected the files to be generated you can click the "Generate" button to create
the files. If all went fine you are done. When you see errors that gii is not able to generate the files you have to
adjust directory permissions so that your webserver is able to write to the directories and create the files.
> Note: The code generated by gii is only a template that has to be adjusted to your needs. It is there
to help you create new things quickly but it is not something that creates ready to use code.
We often see people using the models generated by gii without change and just extend them to adjust
some parts of it. This is not how it is meant to be used. Code generated by gii may be incomplete or incorrect
and has to be changed to fit your needs before you can use it.
11 years ago
Creating your own templates
---------------------------
Every generator has a form field `Code Template` that lets you choose a template to use for code generation.
By default gii only provides one template `default` but you can create your own templates that are adjusted to your needs.
11 years ago
If you open a folder `@app\vendor\yiisoft\yii2-gii\generators`, you'll see six folders of generators.
```
+ controller
- crud
+ default
+ extension
+ form
+ model
+ module
```
This is name generator. If you open any of these folders, you can see the folder `default`. This folder is name of the template.
11 years ago
Copy folder `@app\vendor\yiisoft\yii2-gii\generators\crud\default` to another location, for example `@app\myTemplates\crud\`. Now open this folder and modify any template to fit your desires, for example, add `errorSummary` in `views\_form.php`:
```php
<?php
//...
<div class="<?= Inflector::camel2id(StringHelper::basename($generator->modelClass)) ?>-form">
<?= "<?php " ?>$form = ActiveForm::begin(); ?>
<?= "<?=" ?> $form->errorSummary($model) ?> <!-- ADDED HERE -->
<?php foreach ($safeAttributes as $attribute) {
echo " <?= " . $generator->generateActiveField($attribute) . " ?>\n\n";
} ?>
//...
```
All, in fact our template ready. Now you need to tell GII about our template.The setting is made in the config file:
```php
//config/web.php for basic app
//..
if (YII_ENV_DEV) {
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
'allowedIPs' => ['127.0.0.1', '::1', '192.168.0.*', '192.168.178.20'],
'generators'=>[ //here
'crud'=>[ //name generator
'class'=>'yii\gii\generators\crud\Generator', //class generator
'templates'=>[ //setting for out tempates
'myCrud'=>'@app\myTemplates\crud\default', //name tempate => path to template
]
]
],
];
}
```
Open the CRUD generator and you will see that in the field `Code Template` of form appeared own template .
11 years ago
11 years ago
Creating your own generators
----------------------------
Open the folder of any generator and you will see two files `form.php` and `Generator.php`. One is the form, the second is the class generator. For create your own generator, you need to create or override these classes in any folder. Again as in the previous paragraph customize configuration:
```php
//config/web.php for basic app
//..
if (YII_ENV_DEV) {
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
'allowedIPs' => ['127.0.0.1', '::1', '192.168.0.*', '192.168.178.20'],
'generators'=>[
'myCrud'=>[
'class'=>'app\myTemplates\crud\Generator',
'templates'=>[
'my'=>'@app/myTemplates/crud/default',
]
]
],
];
}
```
```php
// @app/myTemplates/crud/Generator.php
<?php
namespace app\myTemplates\crud;
class Generator extends \yii\gii\Generator
{
public function getName()
{
return 'MY CRUD Generator';
}
public function getDescription()
{
return 'My crud generator. The same as a native, but he is mine...';
}
// ...
}
```
Open gii Module and You will see that added a new generator.