Taste “Single Responsibility” in your laravel controllers with laravel-widgetize package !

Iman Gh
3 min readJan 15, 2018

--

You may already have heard about a famous software design pattern known as SRP (Single Responsibility Principle), which simply says that:

Each class should have one and only one reason to change and be responsible for only one thing…

BUT

You may have always wondered how to practically apply it in your day to day laravel work, I too was confused.

Lets start with a very regular scenario, that we always face in websites.

Imagine the home page of an online-shop. It has many sections. one for slider, Top menu, Recent products, Special offerings, Most-sale products,…

In a typical MVC framework like Laravel we create a Route::get(‘HomeController@homepage’) and start to do the rest of the work…(then create a controller and query the database, return a view, etc)

BUT wait a minute !!!

The Problem :

The homepage method is now responsible for many things ! In fact our homepage method is responsible for providing data for all the sections of the page. oh oh !!! We have violated the SRP a lot.

And not only that, we have also violated the Open-closed principle, because if we decide to add or remove or modify a section from the page. we have to come and tamper with the controller method. In scientific terms our controller method is not closed for modification. :(

So you may get disappointed and think that this is enforced by MVC and these Solid design pattern are not practical and just a bit of theory. I was like you! But finally found the right solution and created a laravel package for it!

Enter the Laravel-Widgetize world to taste the sweetness of Solid design pattern.

Now that we know the problem let’s read about the solution :

The Solution:

The idea for remedy the issue is to create a “Widget” for each section of the page. (in the example : one for slider, another for recent products…)

So what is a widget ? Huh (0_o)

let’s describe the concept of widget.

Widgets are self-contained and encapsulated things and we just include them in our blade files (as easily as including a page partial.)

but the difference is they know how to provide data for themselves and the controller does not have to provide data foreach and every one of them.

Currently you may create a page partial for recent products and write:

@include(‘partials.homepage.recentProducts’)

In your view files. but what ever products you want to display in that partial should come from controller.

With widgets:

@widget(‘recentProductsWidget’)

Note that the controller does not need to fetch recentProducts and pass it as a variable to the view. the ‘recentProductsWidget’ has it’s own controller method to query the database for recent products.

In other words we will have many small widgets, each widget has it’s own controller and each controller will be responsible for only one section of the page.

Beyond that, widget know how to cache repeating results and they perform much faster. They are not stupid being that query the database for the same thing over and over again and compile the page partial with it, They are much smarter than that !

So go ahead and use them. they may rescue your slow running online shop to a much faster and with higher rank in search engines.

--

--