How Would You Implement Solution in Yii

Some background: I’ve been developing in PHP for over 10 years, and just made the transition to Yii last week, loving it so far. I think this is now my framework of choice.

I’m converting my existing backend code to the Yii framework and wanted to get your feedback on how to best implement my solution.

I have various classes that are all very similar, for simplicity let’s say I have a folder of Vehicles: Car, Truck, Semi & Motorcycle.

All vehicles inherit from the Vehicle model.

Each of the Vehicles may or may not appear in the search results, so I thought about using a factory so that if someone searches on a car, they get the Car View, if they search for something that contains both a Car and Semi, the Controller only needs to load the Car and Semi views, doesn’t bother with Truck and Motorcycle.

My main question is: Is my solution an Extension, a Component, a Module or should I just put the controllers in the controller folder, the models in the model folder and the views in the view folder?

Any suggestions on how to build this type of solution?

How would YOU implement? I know that Yii is flexible enough to do any of the above, but I and want to follow the "best-practices" and I wanted to get your suggestions.

Your solution involves models, controllers, views and maybe some 3rd party extensions. So you should put all these components into the corresponding folders or create a module.

I hope it makes things more clear :)

Thanks for your reply, andy_s.

Okay, can you help me better understand when I should build a module vs just using the Yii MVC folders? What considerations do you take into account?

It’s a matter of taste. Modules are supposed to be self-contained application parts, which can be used in many projects. For example, in the extensions you can find the “yii-user” module to manage site users. You just copy it to your application and use. Gii code generator is also a module. Also you can create an “admin” module as a backend part. But for other tasks modules are not necessary. I doubt you will need to manage cars, trucks and motorcycles in other projects, so there is no need to create a module. But if you have a huge application, including blog, news, articles, gallery, etc., then separating it into the modules is a good idea. It makes the application’s structure more clear.

Perfect explanation, thank you…