P
P
pantsarny2015-06-06 15:31:11
Yii
pantsarny, 2015-06-06 15:31:11

Yii2 - how to properly organize the structure?

Good afternoon, colleagues! There was a following situation - implementation of the project on Yii2. It is planned at the beginning of 2 modules - a magazine (magazine) and a catalog (catalog). Based on the yii2-advanced template. The question is how to properly organize the structure. I think it's necessary to implement magazine & catalog modules in every application (backend & frontend). Those. structure:
backend/modules/magazine/...
backend/modules/catalog/
frontend/modules/magazine/
frontend/modules/catalog/
This raises another question - where to store module models?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2015-06-06
@pantsarny

In common - models, in frontend - controllers for frontend, in backend - vice versa.
Those. make the module in common and inherit from it all the rest in the backend and frontend

I
ImPuuLsE, 2015-09-03
@ImPuuLsE

Alexander Makarov Gii would still generate everything as it should, so that this would not happen: 02dc91a99e934d5b93f79bc99539894d.jpg
In general, there would be a fairy tale

M
Maxim Grechushnikov, 2020-06-19
@maxyc_webber

I don't particularly like the topic of modularity. Not such a big project.
Well, first of all, I suggest renaming the magazine to the blog, the context is more suitable. well, it will not confuse less English-speaking developers in the future)
I would suggest using a service architecture layer.
A service is made, for example, common\services\blog\Post.php
in this class, all methods for working with posts. so far only store/update/destroy/findOne/findAll
in the admin panel come to mind, where you need to work with posts, you will work with only 1 of this service.
Namely, in the constructor of the controller responsible for working with posts, we specify
__constructor(...., PostService $postService, ...)
{
...
$this->postService = $postService
and in the store method of the controller, all you need to do to create a post is
$createdPost = $this->postService->store($arrayData)
and move on.
as a result,
- in the controller you only accept data from the user
- validate
- if everything is ok, then send the data to the service. there all the magic is encapsulated
- whatever you do based on the created or not created post
, in no case shove any more logic into controllers or models. only in services.
if any error occurs in the service method, throw an exception and handle it in the controller.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question