Pros and cons of generated classes over manually created ones

Like with every automatic generator, Gii should not be treated as an automatic programmer doing all the work for you.

First of all, Gii obscures the mechanics of CRUD. So, when you automatically generate the controller actions, it'll be difficult for you to understand how they work, especially if you are new to the framework. This is a common issue with all automatic code generators; they are useful only for people who already know what code they need to write and just don't want to write it themselves.

Second, it's possible that you will need a set of absolutely standardized uniform CRUD UI pages for many models. In this case, code repetition induced by Gii in the generated controllers will be completely unnecessary. In Chapter 11, The Grid, we will show you how three controllers generated by Gii can be reduced to one base Controller class and three specific controllers trimmed down to virtually two lines of custom code each.

To sum up, the automatic generation of a CRUD UI in Gii is especially usable only when you need some generic baseline for your actual work, and the changes in index, create, edit, and delete actions are expected. In other cases, maybe it'll be easier to just write controllers from scratch, which is not so hard anyway.

Do note, however, that Gii is not restricted to generating the CRUD UI. Its model generator is virtually priceless, because you certainly do not need to write boilerplate code for your active records, and Gii can infer a lot of information from the table schema for you. Also, it has several other generators, including an Extension generator; we will not be using it in Chapter 9, Making an Extension, though, because it will hinder your ability to understand how things work inside Yii 2.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.212.212