Using Interfaces in Laravel

What is an Interface?

In Object Oriented Programming,an Interface is a contract or boundary where two independent systems are meet or bind to communicate with each other. It has a list of functions that must be found in any object that purpose to “follow” the interface.

As an example, 3 light objects in below (Tubelight, bulbs and LED), should have a turn_on() method and a turn_off() method like:

<?php
class Tubelight implements light{
	function turn_on(){
		// Some logic to switch on light
	}
	function turn_off(){
		// Some logic to switch off light
	}
}

//There is a example of class Tubelight, Whatif we have another lights like bulbs and LED. 
//Then we just do the same thing like tubelight.

class bulbs implements light{
	function turn_on(){  // may be different logic from tubelight }
	function turn_off(){ //logic }
}

class LED implements light{
	function turn_on(){  //logic }
	function turn_off(){ //logic }
}

Interface:

<?php
interface light{
	// NO data or VARIABLES allowed in an interface
	function turn_on();
	function turn_off();
}

A common interface for all types of light.

Let's See How to use it in Laravel :

App/Http/Controllers/BookController.php,  In our controller we can see how clean is it every methods has not more than 2 lines code also easy to understand what is going on in this controller  in each methods. In below code we have several methods.

<?php namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Repositories\BookDbInterface;

use App\Http\Requests\PublishBookRequest;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;

class BookController extends Controller {

    protected $repo;
    public function __construct(BookDbInterface $repo)
    {
        // we istantiated BookDbInterface in $repo and it will work in whole class
        $this->repo = $repo;
    }
 	/**
	 * Display a listing of the resource.
	 * @return Response
	 */
	public function index()
	{
		$allBooks = $this->repo->all();
        return View('books.bookList', compact('allBooks'));
	}

	/**
	 * Show the form for creating a new resource.
	 * @return Response
	 */
	public function create()
	{
		return view('books.addBook');
	}

	/**
	 * Store a newly created resource in storage.
	 * @return Response
	 */
	public function store(PublishBookRequest $requestData)
	{
        $this->repo->insertData($requestData);
        return redirect()->route('book.index')->with('message', 'New record has been added!');
	}

	/**
	 * Display the specified resource.
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
        $book = $this->repo->showOrEditData($id);
        return view('books.showBook')->with('book',$book);
	}

	/**
	 * Show the form for editing the specified resource.
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
        $book = $this->repo->showOrEditData($id);
        return view('books.editBook')->with('book',$book);
	}

	/**
	 * Update the specified resource in storage.
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id, PublishBookRequest $requestData)
	{
        $this->repo->updateData($id, $requestData);
        return redirect()->route('book.index')->with('message', 'Record has been updated!');
	}

	/**
	 * Remove the specified resource from storage.
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
    {
        $this->repo->deleteData($id);
        return redirect()->route('book.index')->with('message', 'Record deleted successfully!');
    }

}

App/Repositories/BookDbInterface.php, There is our interface where all of the methods and passing necessary parameters, Which will operate in BookDbRepository class. Lets see big things :-

Now we can see Our controller and BookDbRepository clasess are 2 different things to perform a single object and even they have no knowledge about  each other works. What they are doing, We also reduce coupling.  Here, Interface is also performing a role communicator. 

<?php namespace App\Repositories;

interface BookDbInterface {
    public function all();
    public function showOrEditData($id);
    public function insertData($requestData);
    public function updateData($id, $requestData);
    public function deleteData($id);
}

App/Repositories/BookDbRepository.php, This class will execute all of the database related works. Where it will implements our BookDbInterface.

<?php namespace App\Repositories;

use App\Book;
use App\Repositories\BookDbInterface;

class BookDbRepository implements BookDbInterface{

    public function all(){
       return Book::all();
    }

    public function showOrEditData($id){
        return Book::find($id);
    }

    public function insertData($requestData){
        $book = new Book;
        $book->title= $requestData['title'];
        $book->description= $requestData['description'];
        $book->author= $requestData['author'];
        $book->save();
    }

    public function updateData($id, $requestData){
        $book = Book::find($id);

        $book->title = $requestData['title'];
        $book->description = $requestData['description'];
        $book->author = $requestData['author'];
        $book->save();
    }

    public function deleteData($id){
        Book::find($id)->delete();
    }
}

App/Providers/AppServiceProvider.php, Its time to bind our repository class to interface class into the register method.

<?php
public function register()
	{
	    $this->app->bind('App\Repositories\BookDbInterface', 'App\Repositories\BookDbRepository');
	}

 

Something to say? Tell us in comment section.