W
W
WebDev2020-08-04 10:35:05
Software design
WebDev, 2020-08-04 10:35:05

Is it customary in Laravel to create a controller for each model?

Let's say I have two tables users and photos. The user uploads photos to their profile. Do I need to create a PhotoController or can adding photos be implemented in a UserController? When does a model need its own controller and when does it not?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
s_k_e_j, 2020-08-04
@s_k_e_j

The controller is the link between the user's actions and the application logic, so it is necessary first of all to focus on the actions that the user performs. If we are talking about processing photo editing, then there should be a PhotoController with an edit () method, etc. If we are talking about changing the username, then this event should be processed by the UserController.
Of course, it is not necessary to create in order to have a controller.

S
Senbonzakuraa, 2020-08-04
@Senbonzakuraa

Why does the UserController need to know anything about photo processing?
You make a separate controller for the photo and that's it, you already write what you want in it, but only as far as the photo is concerned.
If you need to resize the image or somehow work with it, then your user controller will turn into a mess if you sculpt everything into one controller.
The user the controller on that and the user that in it describe everything that concerns the user.
And in general, this is a question of code architecture in general, and not Laravel or PHP

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question