Wednesday 3 May 2017

Topic 17 - Laravel Repository

Introduction


Just to remember laravel repository is not mandatory for laravel projects but represent as collection of specific type of entities where we write those functions and information on interface and then implement them on repositories to provide a cleaner way and easy to read for our controllers and can be used repetitively (over and over) .


Repositories interface --------------- belongs to -----> domain layer
Implementation repositories ------- belongs to ------> application-service layer

Domain Layer 

The domain model layer contains information and functionality related to what it means to be a User. It should map conceptually onto something that exists physically in the real world or a clearly defined concept in the problem space.

Service Layer

The service contains information and functionality related to performing atomic units of work. It should map conceptually onto tasks that are performed on or by members of the Domain model. A single atomic task performed by clicking a button in the application generally involves many members of the Domain working together.

Benefit of Use


1. Using repositories make our controllers less verbose , loosely coupled and easier to read.
2. Represent as concept of storage collection for specific type of entitiy (model)
3. Abstract storage mechanism for authoritative collection of entities.
4. Freedom to swap out data stores at a later date.

Without Repository 

class AlumniController extends Controllers{ public function index(){ $alumni = Alumni:all(); return View::make('alumni.index',compact('alumni')); } public function create(){ return View::make('alumni.create'); } public function show($id){ $alumni=Alumni::find($id); return View::make('alumni.show',compact('alumni')); } }

With Repositories

1: Create the Repository Folder














2: Create class Interface
<?php namespace App\Repositories\Alumni; interface AlumniRepository { public function getAll(); public function show($id); }

3: Create class Repository
<?php namespace App\Repositories\Alumni; use App\Models\Alumni; class EloquentAlumni implements AlumniRepository { private $model; public function __construct(Alumni $model) { $this->model = $model; } public function getAll(){ return Alumni::all(); } public function show($id){ return Alumni::find($id); } }

4: Create Backend Service Provider

<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; class BackendServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { // } /** * Register any application services. * * @return void */ public function register() { $this->app->bind('App\Repositories\Alumni\AlumniRepository', 'App\Repositories\Alumni\EloquentAlumni'); } }

5: Update Providers in Config/App.php

'providers' => [ .
. App\Providers\BackendServiceProvider::class, ],

6: Update Your Controller

class AlumniController extends Controller { protected $alumni; public function __construct(AlumniRepository $alumni) { $this->alumni = $alumni; } public function index() { $alumni = $this->alumni->getAll(); // use function index in alumni interface & repository return view('layouts.template',compact('alumni')); } }

References 


http://shawnmc.cool/the-repository-pattern#disqus_thread
http://vegibit.com/laravel-repository-pattern/
http://stackoverflow.com/questions/31453570/laravel-5-repository-inside-service-provider

No comments:

Post a Comment