Yii 2 MVC

Happy Holidays to everyone around the world, to our wonderful international following, I hope everyone has a great holiday season. I’ve been hard at work on a new writing project, a new book, which I will be releasing details on in the near future. One of the benefits of constantly doing technical writing is that you get to see things from many angles, which often gives you new perspective, even on the most basic things.

Since my blog is oriented towards beginners, I thought I would share some thoughts about the MVC pattern, and why I think Yii 2 does such a great job utilizing and preserving it. And while I’m mentioning Yii 2, a word of thanks to everyone who has bought Yii 2 For Beginners, I really appreciate your encouragement and support, it helps me vigorously pursue this work.

Ok, so jumping into MVC, it’s such a simple idea in many ways, that it quickly gets diluted, or at least our sense of it, depending on how an application is organized. I’m a big fan of an application having folders clearly named models, views, and controllers. in the basic app, these are very easy to find, right there in the root folder.

In the Advanced Application, which is the one I work with, the application has folders for frontend, backend, and common, and within those folders, you find the model, view, and controller folders. I know I’ve mentioned it before, but it bears repeating, the Advanced Application gives you a working user model, with registration, login, and forgot password functionality as well as the frontend/backend separation that I just talked about, so I actually found that easier to work with than the basic app. Just a heads up if you are relatively new to all this.

So from a basic organizational standpoint, Yii 2 is very intuitive and very well defined. Now I know that might sound a little vague, so let me try to be a little more specific and talk about an advantage that Yii 2 has over many other frameworks.

In Yii 2, you do not have to manually define each route. Yii 2 has built-in routing logic that sends the user request to the appropriate controller and view. By itself, that’s pretty cool, it alleviates you from having to keep track of all the different routes moving through the system as well as it relieves you from constantly having to bounce around to a routes file or equivalent. You don’t have to keep track of the path, so no headaches when there is a typo in the route or you forgot to reference something correctly. You can avoid that entirely, so that’s a plus for Yii 2.

But there’s another big plus from Yii 2’s architecture, and this may be a by-product of Yii 2’s built-in route definition, and that is it gives you a sense of a well-defined MVC right out of the box. You get to see how the controllers interact with the views, how they utilize the models, and how it all fits together. In a sense, the framework itself defines the MVC pattern more strongly, and it teaches you a very efficient and effective structure that you can use as you build out your application.

In the advanced application especially, you get enough examples of validation, form models, and the user model, to really see the MVC pattern operating beautifully. You get just the right amount of logic in the controllers to keep them thin. If you were, for example, to extract too much logic out, so you have almost no logic in the controller, you would be constantly drilling dwon into other classes to keep track of what’s going on. If you have too much logic, the core mission of a controller, which is essentially routing, is lost in clutter. Yii 2 finds the perfect balance for you right out of the box.

Another measure of a successful implementation of the MVC pattern is how little PHP ends up in the view file, always a challenge in modern development. If you look at Yii 2’s implementation of a form view, for example, you will be amazed at how clean it is, and how little is actually there. It’s one of many things I love about the framework.

In the MVC pattern, models are supposed to carry the most weight in terms of volume of code, since they do so much. The models in Yii 2 have been consturcted to push a lot of their logic into the parent classes, which you do not have to write, so you get to focus on setting up your validation rules and relationships. For example, when you create a model in Yii 2 from a table, the model knows the attributes, you don’t have to declare them at the top of the class.

Not only is that convenient, but it also keeps the code clean and gives you a great structure that the models will adhere to. Depending on your level of OOP experience, this might not seem as apparent at first, because, as it is with any larger structure, there’s a lot to get used to. Just the sheer size of the framework can be a little overwhelming, expecially for newer programmers or those with less framework experience.

But learning a new framework always involves taking in a lot of new information, and if you think about it, constant learning is one of the defining characteristics of being a programmer, especially given how quickly things can change. Don’t worry, you are not alone in that, and the good news is that is a lot of support out there for Yii 2. A lot of programmers love this framework.

In the months ahead, I hope to be able to bring you more insights and tutorials that will help get the most out of your experience with Yii 2. Once again Happy Holidays to everyone around the world.

And thanks again to everyone who has supported my blog and book. Feel free to contribute with your own comments, links, and reviews, it is greatly appreciated.

Advertisements

Yii 2 Avanced Template

A question I’ve seen on numerous occasions in the Yii 2 Forum is should I use the Yii 2 Advanced Template or the Basic Install? The answer to this depends quite a bit on what you want to accomplish and how. The first thing to mention is that if you are considering this question, you really should install a copy of both to get a hands on feel for yourself.

The instructions for installation of basic are here in the Guide. The instructions for the Yii 2 Advanced Template Installation is here.

Now if you are familiar with this blog, you know that I’m a big fan of the advanced template and that I use it in my book, Yii 2 For Beginners. It’s a little counter-intuitive, but I actually found that easier to work with than the basic application. The reason for this is that in the advanced template, they provide a fully-functional, database-driven user model, which is out of the box ready to register and login users. You even get the forgot password functionality, working out of the box, so you don’t have to code anything for that either.

So I found this to be incredibly helpful when I was new to the framework and I still feel strongly about it now, it’s a great template. The basic application is awesome too, after all, it’s the same framework. And depending on your application needs, it might make sense to start with that.

In my view, if you are just learning the framework, I think it’s easier to get started with the advanced template, even though you have to do a little more work in configuration and with the second host entry for the backend. Any medium to large size project would probably want separation of backend and frontend areas of the application, so the advanced template gives you a working blueprint for that. And considering it was made by the same team that developed the framework, it’s a strong blueprint. You can learn a lot from it.

Although they give you a working user registration and login in the advanced template, it’s only a starting point, and you still have to implement your own RBAC and any special logging that you might want to do, recording the ip address of the user for example.

Yii 2 does include an RBAC component that you can use with either template, and it is now well-documented and feature rich. I don’t use this implementation in my book, going for something simpler because I prefer to be closer to the RBAC code, so I can modify it more easily. It’s one of those things that just seems like I would have so many custom requirements that it’s just easier to go my own way with it. But that’s not to say anything negative about the Yii 2 RBAC, it’s really cool and ready for you to use if it meets your needs. It’s just another awesome feature that the framework provides for you.

In a sense you could apply the above statement to things like the user model, depending on how you wanted to build your application. So for example, if you had some custom implementation of a user model that was just too different from the advanced template, you could simply build it from scratch with the basic application.

Also, the way the backend and frontend are divided in the Advanced Application is obviously not the only way to do it. Some applications will not require a separate backend, though this is probably a minority. In smaller apps, you don’t necessarily even need a separate set of directories for backend, but keep in mind that apps tend to grow over time, so in my view, it’s best to account for this upfront. The advanced template is perfect for this. But you could also structure your backend differently by setting it up as a module.

Personally, I like the Advanced Template, I think it’s the perfect starting point for a medium to large application, or maybe even a smaller application where there is enough of a backend to imagine managing users through a backend UI.

Another thing I really liked about the advanced template, getting back to what I said earlier, is that you get to see a beautiful implementation of the user model that you can learn from, especially when you are just learning the framework.  For example, did you know that the Yii 2 Advanced Template doesn’t use a password field in the DB?  Instead it uses password_hash, and you can get the full details on that by doing the setup.  It’s just a great example of a user model.

In another example, when I wrote my Super Simple RBAC Tutorial, which gets you a working RBAC for the advanced template in 6 easy steps, I looked at the methods on the User model and the form model and followed their format and it really helped, everything just flowed beautifully. The point being that for newer and beginning programmers, learning how the framework does things is very useful and instructive and will help you progress faster in the long run.

If your are part of a team of professional developers deciding which version to use, basic or advanced, just keep in mind that both are equally capable of delivering enterprise level performance. So it just makes sense to see if the Advance Template represents a good starting point or simply will end up getting in the way, and obviously, no one can answer that question for you, you have to decide that for yourself.

Thanks again to everyone from around the world who has supported my blog and book. Feel free to contribute with your own comments, links, and reviews, it is greatly appreciated.