Becoming a better Laravel developer by using the SOLID design principles. Part 1
Tags: laravel, design, solid
Published: 2020-07-17 07:51:17

I'm starting a series of explaining and using **SOLID** Design Principales specifically in [Laravel](laravel.com). 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 

```<?php

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](https://laravel.com/docs/6.x/validation#form-request-validation) 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`.

```<?php

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](https://laravel.com/docs/6.x/validation#form-request-validation). 

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!