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.
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.
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:
-
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.
-
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.
-
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.
Please or to participate in this conversation.