Answer the question
In order to leave comments, you need to log in
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
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.
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.
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.
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.
which wastes additional resources
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question