slimphp / Slim-Skeleton

Slim Framework 4 Skeleton Application
http://www.slimframework.com
MIT License
1.58k stars 477 forks source link

Why are Action classes used over Controller? #124

Closed samuelgfeller closed 5 years ago

samuelgfeller commented 5 years ago

I'm a beginner and am unsure about where I should ask this question. If it's not the right place here let me know and I'm sorry.

So until now I always worked with Controllers and saw that the slim skeleton is with Action classes. My questions are:

Thanks in advance for your answers. I know that you all do that for free, and I am already a big fan of slim, so I'm really thankful for all this!

l0gicgate commented 5 years ago

This is just a design choice. I don't want to say one is better over the other. But in the case that multiple actions require the same dependencies, I would suggest that you create an abstract action and have other actions extend it.

Example abstract class UserAction

You get the idea.

For a big project wouldn't it have a lot of files where it's not handy to find a specific request? Wouldn't it be easier to search in a Controller?

I personally think it's easier to find a singular action than trying to find a method within a large controller. In PHPStorm I just do entire project search Ctrl + Shift + N and type in the prefix of the action, the type of entity and it pops up.

And what would you recommend me to use for a big project with multiple big modules?

I have a project with over 400 actions in it. It's really just a matter of preference.

samuelgfeller commented 5 years ago

Okay thank you very much for clarification

samuelgfeller commented 3 years ago

Relevant argument in favour of Action Classes over Controllers:

Note: Closures (functions) as routing handlers are quite “expensive”, because PHP has to create all closures for each request. The use of class names is more lightweight, faster and scales better for larger applications.

Source: https://odan.github.io/2019/11/05/slim4-tutorial.html