Single Method Controllers in Laravel

Massive brains on July 02, 2019

We all agree that methods and function should do one thing only. We call this Single Responsibility Principle. However, the fact that methods sho... [Read Full]
markdown guide
 

I’ve never did this before... but I do agree that Controllers should be kept lean.
What specific benefits have you found in this pattern? I’m assuming easier debugging but can’t see performance benefits and in my opinion it’s not better developer experience compared to several controllers with a handful of methods that are common to a resource or domain.

 

You'll place all related controllers in the same namespace (App\Http\Controllers\Users{Register,Invite, ...}UserController.php)

As indicated in this article, it adheres more closely to the single responsibility principle, that a given class should only do a specific thing.

Personally, I'm leaning more and more towards ADR where a single method controller ties in perfectly.
I highly recommend looking into ADR more, and see for yourself where the advantages lie for your project.

 

A really nice idea! I generally don't have to struggle with fat controllers because I put all the logic inside what I call repositories (I know technically repositories only persist data but I don't have a better term). As such, my controllers simply validate stuff and pass on the data to these repositories and pass back the success/error responses. Sometimes I think I'm doing it all wrong, while sometimes it feels like a great idea. 🧐

 

I think what you are naturally looking for is the command/handler pattern to encapsulate use cases.

 

I now think that what I'm doing is very close to the idea of services in Symfony. I read that people use services to do both input validation and business logic, which is what my "repositories" are doing. That said, thanks for the command/handler pattern. I'll make sure to look into it next!

 

Good write up!

I read about this technique before - have to admin I didn't use it yet, as I think I'll end up needing more functions from the controller and thus editing everything back.

 

I have used single method controller once in a symfony project, and I've got to admit that it's indeed much more clear/easy to read !

 
 

Sounds totally nice to me, I will try it in my next project!

code of conduct - report abuse