loading...
Cover image for Becoming a better Laravel developer by using the SOLID design principles. Part 1

Becoming a better Laravel developer by using the SOLID design principles. Part 1

abrardev99 profile image Abrar Ahmad ・2 min read

I'm starting a series of explaining and using SOLID Design Principales specifically in Laravel. Today, I will try to cover the first design principle S.

What is SOLID

S.O.L.I.D is an acronym for the first five object-oriented design(OOD)** principles** by Robert C. Martin, popularly known as [Uncle Bob (https://en.wikipedia.org/wiki/Robert_Cecil_Martin).

S.O.L.I.D stands for

S - Single-responsibility principle
O - Open-closed principle
L - Liskov substitution principle
I - Interface segregation principle
D - Dependency Inversion Principle

Purpose of SOLID Design Principales

  1. To make the code more maintainable.
  2. To make the code easier to read and understand
  3. To make it easier to quickly extend the system with new functionalities without breaking the existing ones.
  4. To make the clean code.

Let's look at S - Single-responsibility principle.

According to the official definition of Single-responsibility principle

A class should have one and only one reason to change.

What does that mean? let's understand with an example in Laravel.
Let's assume you have a store method in the controller (for example store method in PostController) and store method look like below with validation


namespace App\Http\Controllers

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Post;

class PostController extends Controller
{
    public function store(Request $request)
    {

        // validate incoming request

        $validator = Validator::make($request->all(), [
           'title' => 'required|string|max:200',
           'description' => 'required|string',
       ]);


       // finally store our post

    }
}

let's implement the Single-responsibility principle here, Laravel provide Form Request object out of the box. Make Form Request using an artisan command

php artisan make:request StoreBlogPost

Which will create new Request class in app\Http\Request\StoreBlogPost.

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreBlogPost extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
           'title' => 'required|string|max:200',
           'description' => 'required|string',
        ];
    }

}

Now change our PostController to use our StoreBlogPost.


namespace App\Http\Controllers

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Post;

class PostController extends Controller
{
    public function store(StoreBlogPost $request)
    {

       // finally store our post

    }
}

It will automatically validate the request data and return validated requests. Now you can $request the same as you use the Request object. You can do a lot of other stuff with Form Request like custom messages, custom validations, etc for more visit Larave official documentation.

If you like the article please consider like and follow me, there are 4 other parts coming understanding SOLID and using in the Laravel app.

Cheers!

Discussion

pic
Editor guide
 

i didn't understood in the end. you shifted the focus to single responsibility for Laravel form \request though it was suppose to be an example. if I create a form request how single responsibility is implemented?

 

As I explained in article, we are going to see SOLID principle in context of Laravel. So I gave example from Laravel and how to achive "every entity should have single responsibility".

As controller store method meant to store data not validation of data.

So we extract validation logic to seprate class called Request and in this way we implemented single responsibility principle.

Although there a tons of examples with general purpose code and with Laravel. But with laravel it is simple and easy to understand example.

 

now as you mentioned in comment "As controller store method meant to store data not validation of data.

So we extract validation logic to seprate class called Request and in this way we implemented single responsibility principle."

now its clear..

Best of luck

 

Great tip, congrats! I really wanna read more. ;)

 

Thank You. I will be writing more.

 

I have just published the part 2 of this article of O: Open Close Principle

dev.to/abrardev99/becoming-a-bette...