Yii2 Advanced Model?

Hi, I’m very new here. And I want to ask a question about yii2 advanced:

Should I use a model (store in common) for both backend/frontend? Like Products model, so I can call it to CRUD for backend and showing products for frontend…

Thanks.

Yes, you can do that.

Put your AR models in "common/models". So you can access them from both ends.

The purposes of common is exactly this, share common code/configuration between different application (frontend, backend api etc etc)

A suggestion: create an empty model in your application to extend the one in common.

So if in future you need to do some kind of personalization just for the specific application you don’t need to refactor the code.

When you generate your Model, then you can save it in common/models. Let’s say you have created a Post model. As your application grows, you will start to mix functions for the backend and frontend into one file. [size=2]You will realize that some of the functions are almost the same, but then you have to keep both of them because where clauses are not the same for backend and frontend etc… You will probably experience function naming problems, some functions will probably have “backend” word in their name, and some of them “frontend”. [/size]

[size=2]The problems are arising if you introduce another module in your project, or 10 other modules and not only Frontend and Backend. [/size]

[size=2]

[/size]

To resolve this, keep your "base" model class called Post in common/models, but in your Frontend and Backend your should create a new model class called FrontendPost, BackendPost which are extending your base model.

This way, your custom functions are separated between Backend and Frontend, you can separate rules() function for Frontend, and Backend, you can also give different label names or any other setting.

Sometimes you would not like to do this, but sometimes it is really helpful to easily maintain the project.

Contributing with my 2 cents.

You could put there your controllers and some components that you use in both ends.

To further add to this, you can even create common views / layouts ;)