Laravel Interfaces Laravel Interfaces laravel laravel

Laravel Interfaces


In my recent laravel 5 project, I'm used to prepare my logics as Repository method. So here's my current directory structure. For example we have 'Car'.

So first I just create directory call it libs under app directory and loaded it to composer.json

"autoload": {        "classmap": [            "database",            "app/libs" //this is the new changes (remove this comment)        ]    }

after that I create a subfolder call it Car . Under the Car folder create two file 'CarEloquent.php' for eloquent implementation and CarInterface.php as interface.

CarInterface

namespace App\libs\Car;interface CarInterface {    public function getAll();    public function create(array $data);    public function delete($id);    public function getByID($id);    public function update($id,array $data);}

CarEloquent

namespace App\lib\Car;use App\lib\Car\CarInterface;use App\Car; //car modelclass CarEloquent implements CarInterface {    protected $car;    function __construct(Car $a) {        $this->car = $a;    }    public function getAll(){        return $this->car->all();    }}

Then create Car Service Provider to bind ioc controller.For create Car service provider you can also use php artisan command by laravel. php artisan make:provider CarServiceProvider

ServiceProvider

namespace App\Providers;use Illuminate\Support\ServiceProvider;class CarServiceProvider extends ServiceProvider {   public function register() {        $this->app->bind('App\lib\Car\CarInterface', 'App\lib\Car\CarEloquent');    }}

And final step would be add these service provider to config/app.php provider array.

'providers' => [  'App\Providers\CatServiceProvider',]

And finally we are ready to use our repository method in our controller.

Example Controller

namespace App\Http\Controllers;use App\lib\Car\CarInterface as Car;class CarController extends Controller {    protected $carObject;    public function __construct(Car $c) {        $this->carObject = $c;    }    public function getIndex(){        $cars = $this->carObject->getAll();        return view('cars.index')->with('cars',$cars);    }}

Main purpose to achieve here call repository method to controller, however you need use them as per your requirement.

Update

CarEloqent basically help us to improve database implementation, for example in future if you want to implement same functionality for other database like redis you just add another class CarRedis and change implementation file path from server provider.

Update 1: Good Resource

http://programmingarehard.com/2014/03/12/what-to-return-from-repositories.html

[book] From Apprentice to Artisan by Taylor Otwell

Very good explanation about repository method and software design principle commonly called separation of concerns. You should read this book.

If you still have any confusion to achieve these behaviors let me know and however I will keep eye on this question to update this answer, if I find some things to change or update or as per requirement.