Category Archives: Gii

Yii 2 Gii code generation

One of things about Yii 2 that is drastically different from some of the other frameworks is Gii, it’s code generation tool. I cover the use of Gii extensively in my book, Yii 2 For Beginners.

On the surface, this might just seem like a fun feature to use, a time-saver in certain circumstances. And that’s probably true of a lot of code generators, they only take you so far. But Gii is different.

Gii is so powerful that it has very meaningful impact on work flow. And this impact reaches deep into our profession. You can look at it from two perspectives, one, the individual developer doing work for clients, and two, from a team perspective, which can bring you up to full enterprise level.

So let me talk first about how it impacts the individual, and since a lot of my readers are beginners, it will help them understand the importance of Yii 2 and Gii.

1. Planning. Use of Gii impacts planning. Before you code a single line of code, you can factor in not only the efficiency of Gii, but also the workflow.

For example, to start, you look at the scope of the project and begin sketching out the table structure. The more well-thought out this data-structure is, the useful Gii becomes. If you use foreign keys to relate tables, Gii will understand this and generate relationships in the models.

This gives you the ability to imagine the project, and better yet, diagram the tables in MySql workbench, before any PHP is involved. And once you know the number of tables you are working with, you will have some idea of how long basic development will take. This is possible because you can average how long each table takes to convert to model to controller to view.

In a typical implemenation, there are 8 files generated. And they also typcially need customization, but once you have an idea of what you generally use for that, even the 1st level of customization goes quickly. When I say quickly, I mean crazy fast. It’s awesome. Awesome is not a technical word, but you have to give me a pass on that one, I really love Gii.

2. Strong MVC Pattern. The code that Gii generates is a beautiful implementation of the MVC pattern. It’s probably the easiest structure to follow of all the frameworks because you literally get folders named models, controllers, and views. And once you understand how it’s all stiched together, it just makes perfect sense.

3. Learn Good Practices. As your workflow and project planning are impacted by Gii, you begin to develop a discipline to the structure. While you are certainly not locked into anything, if you follow the breadcrumbs of good practices that Yii 2 and Gii are putting out there for you, your project becomes very tight and focused. Things are always done the same way, at least at the base level.

It’s understood of course that I’m talking about the bones of the project, not the skin. Presenting the data in the views often requires a lot of customization, but of course Yii 2 helps you bigtime there too with its widgets. But the widgets will have to be the subject of another post.

4. Grow As A Developer. As an individual developer, you build value into your skill set by following best practices. Today you are an individual developer or student. Tomorrow, you may work in a team environment. And those looking to hire programmers are looking for programmers that can follow a discipline and they’re most likely looking for programmers who have framework experience. And the good news is that this is a very accessible framework. You can learn it and benefit from it.

And that gives us the perfect transition to talk about team development. All the benefits that individual developers get from using Yii 2 and Gii are amplified in team and enterprise development. While time estimates are also important to individual developers, they can be absolutely critical in a company environment, where commitments are made to clients or to the backers of a venture.

So you can look at the list of benefits like this:

1. Workflow Standardization. The planning and flow of the project always follows the same path. Team members are assigned specific roles, which is certainly not unusual, but knowing the power of the framework and the capabilities of Gii enhances the strategic deployment of those resources.

2. Common Starting Point For Code. If the conversation starts with data-structure, it will lead to using Gii, in most cases. Obviously there are exceptions to everything. But to the extent that Gii gives us a common starting point for code, it gives the security of building a proven structure. The tight association it creates between model, controller, and view is not something you want to go out of your way to break, unless you have very good reason to do so.

3. Rapid Prototyping. Gii gives you the ability for rapid prototyping. Companies rarely have the luxury of asking “what if?” But with Gii, you can do that. You can even set aside a prototype branch with a separate DB, and playout those whatif scenarios quickly. No company would ever do that unless they could do it quickly and efficiently. But having that ability can give you a competitive advantage, so this is great.

4. Security Of Standardization. Lastly, I should state one of the most obvious benefits to the standardization that both Yii 2 and Gii offer. What if someone leaves the team? Someone else will have to take over their responsibilities, and wouldn’t it be better to know that they can do that efficiently because the structure and discipline of Yii 2 and Gii were adhered to? Of course it would. That’s a big reason why companies use frameworks in the first place.

Besides all the reasons I stated about the benefits of using Yii 2 and Gii, I thought about why it is so good at what it does and why I haven’t seen a competing framework offer something as compelling, at least not from my point of view. And I think the answer is that Yii 2 has it’s own native ORM implementation, which just makes their code generation seemless.

Having their own native ORM also gives Yii 2 the freedom to structure their MVC implementaion how they wish, so they’re able to keep it intuitive, without having to make sacrifices because of 3rd party ORM or other considerations. I believe this is a huge advantage and is one reason why Yii 2 is able to offer so much out of the box. If you haven’t tried this framework, I would strongly encourage you to do so.

I’d like to take a moment and thank all the programmers from around the world who are reading my blog, I hope you are finding it useful. And thanks again to everyone who has supported my book. Feel free to contribute with your own comments, links, and reviews, it is greatly appreciated.

Advertisements