S
S
smigles2019-11-06 03:02:05
PHP
smigles, 2019-11-06 03:02:05

Why are there multiple actions in PHP MVC framework controllers?

In PHP frameworks (perhaps this also applies to frameworks in other languages) that implement the MVC pattern, controller classes consist of several action methods. In most cases, after defining the route in the controller, only one action is needed, but the class is initialized as a whole, which consumes additional resources.
In this regard, the question arises: why combine several actions into one class? Why not make all actions single? The only reason I see now (at least in the Yii that I use) is the desire to put similar actions in the same container.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
G
Gip, 2019-11-06
@Giperoglif

separating actions is the economy of matches and the contradiction of encapsulation. there, besides this, you have so much to initialize that one extra method in the controller is nothing at all.

I
index0h, 2019-11-06
@index0h

Usually, this is just convenient, since actions with one entity can be placed in one controller, or they can be combined according to some other attribute. It happens that several actions use a common private method, which makes no sense to take out in a separate service, and you don’t want to duplicate it between files.
In the case of pulling controller dependencies from the container, there is no gain from dividing the controller into actions, from the word "absolutely".
If controller dependencies are inserted via DI, the benefit will be that there will be no need to forward dependencies that are not specifically needed in this action, and unit testing will also be simplified.

S
Sergey Nizhny Novgorod, 2019-11-06
@Terras

The real reason why each root is taken out to a separate controller is the testability of the code. Those. if conditionally the class contains 5 roots, for each root some separate service with logic is required, plus some auxiliary one (EventDispatcher, etc.), then it is very difficult to test all this later.
If there are few dependencies, or they all go to one subservice, then you can cram everything into one class into different actions - why not.

V
Vitsliputsli, 2019-11-06
@Vitsliputsli

why combine multiple actions into one class? Why not make all actions single? The only reason I see right now (at least in the Yii I use) is to put similarly themed actions in the same container.

Because almost all frameworks use OOP, and MVC has nothing to do with it. Replacing all methods with classes is a crazy fantasy, and it's not about finer fragmentation or assembling similar things together, there is a fundamental difference between an object and a method. The controller is the same object, its actions are methods of this object. How many methods an object has depends on its logic, and not on data division (maybe 1, maybe 10, maybe 0).

A
Anton R., 2019-11-06
@anton_reut

which wastes additional resources

Are you worried about a few bytes in server memory? Do not worry.
So that THEN it would be intuitively clear where to look for the desired action. It's like shelves in a library - it's faster to find a book by shelf number than to make a shelf for each individual book.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question