Laravel: The Fantastic 4 Interfaces — Responsable

Leaning the Response code, one implementation at a time.

Italo Baeza Cabrera
3 min readAug 5, 2019

Credit where is due. Last year I stumbled upon an article of Josip Crnković, in which he walked through some of the useful interfaces the framework has. In it, he discovers some that are used to send a Response to the browser.

After giving them a shot, I have to say these alleviates a lot of DRY problems and slims down multiple lines of code to just a few. In this series of articles I will check them out, as these may help you to code more easily your application.

Responsable

Have you ever wished to lean your Http Controllers full of frankenstein code? Are you tired of creating the Response every freaking time? Then you need the Responsable interface!

The Responsable interface passes the responsibility of creating a Response from the Controller to the class you are throwing out.

The toResponse() method receives the instance of the current Request, and you will be able to build and return the Response as you see fit: using views, returning JSON, whatever.

Using this allows more control on the class that is being sent to the browser, specially when the same class can have different states depending on the data it processes, avoiding building the same Response every time.

Where the magic happens?

The important code is in the Router. When the controller spits out something, the framework will check if is a Response instance. If not, in the last instance it will create a Response and assume the controller has just returned the content to put inside it, like a string.

As you can see, the first thing it does is to check if what the controller outputs is an instance of Responsable. If it is, it will call the toResponse() method, and further prepare what the browser will receive.

Example: Processing a Podcast

In my Podcast application, the Controller receives the uploaded file and then is sent to a PodcastProcessor. This class will pass it to a Transcoder, and it will return the initial status of the transcoding that will vary on the complexity and system load: idle (not started), pending, queued, serviced (to a third-party) or incompatible.

The key is on the toResponse() method. In this example, the PodcastProcessor class will process a Podcast, and will build a Response depending on the result.

Dear application: If I return the Podcast Processor, It’s because I want to know the processing status.

This is also very useful when you want to return the same class across multiple Controller methods, in this example on uploading a Podcast for the first time, and for re-uploading a Podcast. Instead of building the returning Response in each method, you can just simple return the class in your Controller and call it a day.

My Controller now weights just 45 lines.

Of course this is just a silly example, but consider pushing as Responsable a class that changes its shape: instead of reading each property in every method and building a Response depending on the shape of the class, you can just use the toResponse() inside that class , even access protected properties or methods.

Also note, because you have access to the Request instance, you can even change the Response based on the Request information, like the input or if the User is authenticated, and even change the Response headers, without getting outside the Responsable class.

📝 Read this story later in Journal.

👩‍💻 Wake up every Sunday morning to the week’s most noteworthy stories in Tech waiting in your inbox. Read the Noteworthy in Tech newsletter.

--

--

Italo Baeza Cabrera

Graphic Designer graduate. Full Stack Web Developer. Retired Tech & Gaming Editor. https://italobc.com