yaxano's avatar

Is there any performance issue or other con when using exclusively single action controllers?

I am thinking about going 100% single action controllers (via a __invoke() method) way of bulding websites because on a small screen of my laptop it's hard for me to orient in long controllers and for some reason I find easier to work with like that. I already type out every single "action" in the router file so, actually the router will be a bit cleaner.

The only thing holding me down is the possible performance hit of having 60 instead of just 10 controller files, or something like that.

Are there any performance issues connected to having so many controllers?

Also are there any other cons?

Thank you in advance.

0 likes
5 replies
Tray2's avatar

Lots of files = slower performance, but I don't think you need to worry unless you have more than 1000 files per directory. I started using single action controllers in my project and I haven't seen any noticable difference in performance.

1 like
hajat's avatar
hajat
Best Answer
Level 7

Single action controllers in Laravel are a useful way to define simple controller actions without having to create a separate controller class for each action. However, there are some potential downsides to using exclusively single action controllers:

  1. Reduced readability: If you have a lot of single action controllers, it can make it more difficult to understand the overall structure of your application. It can be harder to see how different actions are related to each other if they're spread across many different files.

  2. Decreased flexibility: Single action controllers are best used for simple, standalone actions. If you need to add more complex logic to an action, you'll likely need to convert it to a regular controller method. This can add extra work and complexity to your codebase.

  3. Route collisions: If you have multiple single action controllers with the same name (e.g. "HomeController"), you may run into route collisions. Laravel will automatically generate route names based on the controller name, so if you have two controllers with the same name, you'll end up with conflicting route names.

Overall, single action controllers can be a useful tool in your Laravel toolkit, but it's generally best to use them in moderation and for simple actions only. For more complex actions or actions that need to share functionality, it's better to use regular controller methods.

1 like
Snapey's avatar

a request only uses a single controller. You are saying is it quicker to load 1 file out of 20 or 1 file out of 200

The difference would be imperceptible, especially so as the class is declared in the route definition - its not got to be searched for...

1 like
yaxano's avatar

Hmm, yeah, based on your replies, guys, I won't make the switch. And I will use single action controllers only for some very specific things.

Snapey's avatar

@yaxano Interesting that you chose to accept the AI generated answer posted by @hajat

The points are in most cases flawed, and point 3 is just plain wrong.

Please or to participate in this conversation.