18
loading...
This website collects cookies to deliver better user experience
Repository Pattern
in a Laravel application is to create a bridge between models and controllers. This pattern keeps your Laravel code clean and safe, it is worth using repositories to separate the responsibility for which the model should never be responsible.Especially, you do not need binding the interface and repository class in Service provider class
cuongnd88/lara-repository
using Composer.$ composer require cuongnd88/lara-repository
config/app.php
<?php
// config/app.php
return [
// ...
'providers' => [
// ...
Cuongnd88\LaraRepo\LaraRepoServiceProvider::class,
]
// ...
];
make:repository
commandphp artisan make:repository --interface=User/UserInterface --repository=User/UserRepository --model=Models/User --controller=User/UserController
--interface
is to indicate the Interface file.--repository
is to indicate the Repository file.--model
is to allocate the Model. If not exists, it will confirm to create a model.--controller
option is to create the Controller file. With @resource
, it generates a resource controller class.code less
by running the command:php artisan make:repository --interface=Staff/StaffInterface --repository=Staff/StaffRepository --model=Models/Staff --controller=Staff/StaffController@resource
Repositories
directory is created when you run the command. All interface and repository files are stores in the app/Repositories/Language
directory.StaffController
, StaffInterface
already injected into the construct
method. This action is called Dependency Injection
<?php
namespace App\Http\Controllers\Staff;
use App\Http\Controllers\Controller;
use App\Repositories\Staff\StaffInterface;
use Illuminate\Http\Request;
class StaffController extends Controller
{
protected $staffInterface;
public function __construct(StaffInterface $staffInterface)
{
$this->staffInterface = $staffInterface;
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
repositories.php
in config
directory<?php
//config/repositories.php
return [
\App\Repositories\User\UserInterface::class => \App\Repositories\User\UserRepository::class,
\App\Repositories\Staff\StaffInterface::class => \App\Repositories\Staff\StaffRepository::class,
\App\Repositories\Language\LanguageInterface::class => \App\Repositories\Language\LanguageRepository::class,
];