DEV Community

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

Posted on

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

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

    }
}
Enter fullscreen mode Exit fullscreen mode

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',
        ];
    }

}
Enter fullscreen mode Exit fullscreen mode

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

    }
}
Enter fullscreen mode Exit fullscreen mode

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!

Latest comments (7)

Collapse
 
msamgan profile image
Mohammed Samgan Khan

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?

Collapse
 
abrardev99 profile image
Abrar Ahmad

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.

Collapse
 
msamgan profile image
Mohammed Samgan Khan

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..

Thread Thread
 
abrardev99 profile image
Abrar Ahmad

Best of luck

Collapse
 
abrardev99 profile image
Abrar Ahmad

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

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

Collapse
 
thshiro profile image
Thalisson Barbosa

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

Collapse
 
abrardev99 profile image
Abrar Ahmad

Thank You. I will be writing more.