Apply The Open Closed Principle in Laravel | Part 1

Apply The Open Closed Principle in Laravel | Part 1

Posted on:July 15, 2019 at 10:00 AM

I think one of the most popular design principles is Open Closed Principle that most of the PHP developers use in their project. I am going to show you how to use the Open Closed Principle in Laravel. In order to write deeply, this article might have a few sections. Today, I am going to show you the first part.

Table of Contents

Open Table of Contents

What is exactly Open-closed principles?

In my word, it tells you that, a method is open to adding new logic in your code (Open for extension), but you can not modify the existing code for adding new logic (Closed for modification).

Real-life Scenario

Imagine you have an admin panel, where you have to create CRUD system for Posts, Users, Categories and so on. To solve this issue, normally we create separate controllers for each and do individual CRUD operation on that, right? For example-

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UsersController extends Controller
{
    public function index()
    {
    	# code...
    }

    public function create()
    {
    	# code...
    }

    public function store(Request $request)
    {
    	# code...
    }

    public function show($id)
    {
    	# code...
    }

    public function edit(Request $request, $id)
    {
    	# code...
    }

    public function delete($id)
    {
    	# code...
    }
}

I am sure, many developers like me think this way. Actually nothing wrong with this concept.

But, I have a different opinion. This way might be ok if you write one CRUD controller/operation like users only. How about if you have more than one CRUD operation like users, posts, categories and so on?

In my opinion, of course, it’s not a good practice to write your code this way if you have more than one CRUD implementation. To overcome this challenge, I suggest implementing Open-Closed Principles that allow us to write maintainable code. Let’s draw the picture.

What we need?

We are going to implement this structure in Laravel. I expect you to have a Laravel installation. Now, we need to adjust database connection with Laravel in .env file. Then migrate the database with-

php artisan migrate

Then add some record into the users table.

Now we need the following structure in your App\ folder.

  • Interfaces/CrudInterface.php
  • Controllers/UsersController.php
  • Repositories/CrudRepository.php
  • Repositories/UsersRespository.php

Let’s start-

First of all, let’s update the route in web.php file-

<?php

Route::resource('users', 'UsersController');

Now let’s create the UsersController-

php artisan make:controller UsersController

Now create a folder called Interfaces inside the Http folder and then add a file called CrudInterface. I have defined all the possible methods inside the interface. It should be like this-

<?php namespace App\Http\Interfaces;

use Illuminate\Http\Request;

interface CrudInterface{
	// Show list of the user
	public function index();

	// Show create form
	public function create();

    // Store a new record
    public function store(Request $request);

    // Show edit form
    public function edit($id);

    // Update an existing record.
    public function put(Request $request, $id);

    // Delete a record
    public function delete($id);
}

Now whoever wants to implement that, they need to follow this structure in their class. In our case, we want to implement CrudInterface in the UsersRepository. Then let’s create UsersRepository inside Repositories folder.


<?php
namespace App\Http\Repositories;

use App\Http\Interfaces\CrudInterface;
use App\User;
use Illuminate\Http\Request;

class UsersRepository implements CrudInterface
{
    // Show list of the user
    public function index(){

    }

    // Show user create form
    public function create(){

    }

    // Store a new user record
    public function store(Request $request){

    }

    // Show User edit form
    public function edit($id){

    }

    // Update user's existing data.
    public function put(Request $request, $id){

    }

    // Delete a user
    public function delete($id){

    }
}

So far, the structure is clear enough, right? You will get the code in the GitHub repo.

Well, today I am going to stop here. I will continue the rest of the part in the next episode. If you have any confusion or query, let’s discuss in the comment section.

Apply The Open Closed Principle in Laravel | Part 2

Thank you.