What code should go, where in the MVC structure

My problem is somewhere between the model and the controller. Everything works fine for me when I use MVC only for crud (create, read, update, delete). I have separate models for each database table. I access these models from the controller to crush them. For example, in the contacts application, I have actions (create, read, update, delete) in the controller (contact) to use the model (contact) methods (create, read, update, delete).

The problem starts when I try to do something more complex. There are several complex processes that I donโ€™t know where to put them.

  • For example, when registering a user process. I canโ€™t just end this process in the user model, because I have to use other models (sending letters, creating other records for users through other models), as well as many complex checks using other models.
  • For example, in some complex search processes, I have to turn to many models (articles, videos, images, etc.).
  • Or sometimes I have to use apis to decide what I will do next, or which database model I will use to write data.

So, where is the place to perform complex processes. I do not want to do them in controllers, because sometimes I have to use these processes in other controllers. And I do not want to put this process in the model, because I use models as access levels to databases. Maybe I'm wrong, I want to know. Thanks for your reply.

+5
source share
6 answers

For simple tasks, I would write action helpers (e.g. sendNewsletter).

For complex tasks, I create services (e.g. email, auth, etc.).

+1
source

( ) AFAIK, - MVC - , , , . - , ( ), . , ... . , , - " ".

+3

MVC ( ).

HVMC (, ) .

+1

.

, . , .

, , . , , . 5 6 ... 5 6 , , , .

+1

, ? MVC, ? , "register user".

, SearchController ArticleModel, ImageModel VideoModel? . SearchController SearchModel, .

MVC -, , IMHO - . , " " :

    • ,
  • UserModel
  • UserModel
  • /

, , / .

0

Keep the controllers clean. For backend processing, use Manager classes such as MailManager, etc.

0
source

All Articles