Yii:Customize default gii base crud

Yii is a MVC Framework of PHP.

Yii framework offers default crud generation feature. If you write your application name/gii, it will show default crud generation operations. Many times we use different template in project than default crud and need to made changes after watch time crud is generated. This is post to customize default crud to get rid of redundant task.
Let’s begin.

When you are writing in your application-name/gii ,you are seeing all generators on index.
This generator code is located at  yii/framework/gii/views/index.php
Here ,each generator link related code located at yii/framework/gii/generators,
We will extend basic crud and do change in it, so we are able to generate default crud in other applications.

To add your crud generator follow below simple steps:
1. Make folder with your custom name,suppose mycrud under yii/framework/gii/generators.
Make Code file,like myCrudCode in mycrud,which extends CrudCode.
Import
Yii::import(‘gii.generators.crud.CrudCode’);

2.Make generator in giiextended folder,like MyCrudGenerator, which extends CrudGenerator.
You need to change in $codeModel ,which give path to code file,
here myCode.
Import : Yii::import(‘gii.generators.crud.CrudGenerator’);
$codemodel=giiextended.myCrud.myCrudCode.
Also make it in mycrud,where $codemodel=ext.giiextended.myCrud.myCrudCode
please notice that,if folder name is mycrud,generate will be name of MycrudGenerator,first letter will be capital.

3.Make Folder template in mycrud folder,in which all views and controller  in default folder.Here what you will make changes accordingly future views are generated from your crud.Following views need to be generated as per Yii crud, you can skip 1 or 2 views and change it as per your need, but you need to reflect changes in controller accordingly.

  •   _form.php
  •   _search.php
  •   _view.php
  •  admin.php
  •  controller.php
  •  create.php
  •  update.php
  •  index.php
  •  view.php

4.Make a folder view,put a index.php in it,
copy it form crud(framework/gii/generators/crud/view/index.php) ,change name in h1 tag.

5.In config/main.php,in gii in generatorpath array,add ext.giiextended.
as example:
‘generatorPaths’=>array(
//’ext.gii-extended’,
),
This will responsible for showing uour customized crud name when you type application-name/gii in url. So try it and you will come out of hustle of each time crud change as new table is generated.
Similaraly,you are able to extend model and form generator also of gii.
Start doing it.All the Best!!!
Have a good day!!!

Advertisements