Apply The Open Closed Principle in Laravel | Part 2

Apply The Open Closed Principle in Laravel | Part 2

Posted on:August 2, 2019 at 10:00 AM

In this post, we will learn how to implement the Open-Closed Principle in Laravel. This is the continuation of the previously posted article Apply The Open Closed Principle in Laravel | Part 1 . I highly recommend you to read the previous one before continuing this.

Table of Contents

Open Table of Contents

Let’s Start

Now, here I want to show the index page for users by using the Open-Closed Principle. First of all, in the index() method of UsersController you have to instantiate the CrudRepository.

UsersController.php


public function index()
    {
    	$crud = new CrudRepository(new UsersRepository());

        return $crud->index();
    }

Notice here, I instantiated CrudRespository and then put on $crud variable. Then I call the method ->index() from CrudRepository.

Now, I will navigate to the CrudRepository and create a constructor to inject CrudInterface there. Here should be the code.

CrudRepository.php

protected $crudInterfac;

    public function __constrct(CrudInterface $crudInterface)
    {
        $this->crudInterfac = $crudInterface;
    }

Now, I want to show some message from the index method in the CrudRepository.

public function index(){
        return "Return the view page from the index";
    }

Easy, right? So far we have done. Now, if you run your server and then try to access http://127.0.0.1:8000/users, you should able to see a plain text-

Return the view page from the index

Now, let’s review. What is the advantage of using repository and interface on the open-closed principle? Legit question, right?

Well, now let me explain why it is better over the procedural way of calling.

Just imagine now, you want to add more crud operation on your system. For example, category. In that case, you just need to add route resources in web.php file, like this-

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

Then, create a controller called CategoriesController and then instantiate CrudRepository inside the index method, like what we did in UsersController. Here is the code for CategoriesController .

public function index()
    {
    	$crud = new CrudRepository(new UsersRepository());

        return $crud->index();
    }

The final thing is to create a CategoriesRepository and the code will be like this-


<?php
namespace App\Http\Repositories;

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

class CategoriesRepository 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){

    }
}

Once done, now you are able to access the index method of CategoriesController and you will get the output like that.

Return the view page from the index

In the same way, you can add more and more crud operation for different types of data.

Maybe you can explore more in show() or store() method that how to implement the Open-closed principle there.

You will get the code here

Thank you.