DEV Community

Single Method Controllers in Laravel

Segun Olaiya 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...
Collapse
 
alexparra profile image
Alex Parra

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.

Collapse
 
dealloc profile image
Wannes Gennar • Edited

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.

Collapse
 
nyc4m profile image
Baptiste Prunot

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 !

Collapse
 
bertheyman profile image
Bert Heyman • Edited

Good write up!

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

Collapse
 
slifin profile image
Adrian Smith

methods !== functions

Collapse
 
jorge_rockr profile image
Jorge Ramón

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

Collapse
 
carlosas profile image
Carlos Alandete Sastre

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