An abstract base repository with predefined common features.
The repository pattern has several objectives :
- Encourage development good practices (separation of concerns, code reusability, ...)
- Improve code testability
Before using this package, you should be familiar with the repository pattern, and especially with its Laravel implementation.
You can know more about it by reading the several articles you'll can find on Internet about this.
Here is one among others : https://medium.com/@jsdecena/refactor-the-simple-tdd-in-laravel-a92dd48f2cdd.
Notes :
- This base repository does NOT allow you to manipulate the model : it can sometimes be tempting to directly manipulate the model from your controller but this is not recommended and recognized as a bad practice.
- You should always fill your repositories interfaces : it can avoid huge errors on your projects.
- The provided methods are shortcuts to avoid you to declare them in your own base repository or in several repositories. Keep in mind that they only are pre-defined methods and that you should declare new methods in your repositories if they does not fit with your needs.
The repository pattern setup is not complicated but requires several steps to be accomplished.
Follow them one by one :
- Install the package with composer :
composer require okipa/laravel-base-repository
-
Create a
app/Repositories
directory where you will store your different project repositories. -
Create your
app/Repositories/BaseRepositoryInterface.php
interface and yourapp/Repositories/BaseRepository.php
abstract class :
<?php
namespace App\Repositories;
interface BaseRepositoryInterface extends Okipa\LaravelBaseRepository\BaseRepositoryInterface
{
// add here your own custom method contracts (if necessary).
// they will be implemented in all your repositories.
}
<?php
namespace App\Repositories;
abstract class BaseRepository extends Okipa\LaravelBaseRepository\BaseRepository implements BaseRepositoryInterface
{
// add here your own custom method declarations (if necessary).
// they will be implemented in all your repositories.
}
- Create a first
UserRepositoryInterface
and its associatedUserRepository
:
<?php
namespace App\Repositories\Users;
interface UserRepositoryInterface
{
// add here the users method contracts.
/**
* @return void
*/
public function test();
}
<?php
namespace App\Repositories\Users;
use App\Repositories\BaseRepository;
use App\Models\User;
class UserRepository extends BaseRepository implements UserRepositoryInterface
{
protected $model = User::class;
// add here the users method declarations.
public function test()
{
\Log::info('test');
// manipulate your model as needed. Example : $this->model->create(['email' => 'whatever@email.test']);
}
}
- Create your project
app/Providers/RepositoryServiceProvider.php
file. You can follow the example below :
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Repositories\User\UserRepositoryInterface;
class RepositoryServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
// users
$this->app->bind(UserRepositoryInterface::class, UserRepository::class);
// then, register all your other repositories here ...
}
}
- Add your
RepositoryServiceProvider
to the providers declarations of the Laravel framework, in theconfig/app.php
:
// ...
'providers' => [
// other provider declarations ...
// custom providers
App\Providers\RepositoryServiceProvider::class,
],
// ...
- Add a
$repository
attribute to yourapp/Http/Controllers/Controller.php
base controller that all your controllers will extends :
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
protected $repository;
}
And you're done !
You can now use your UserRepository, empowered with the pre-defined methods provided by this package.
In your app/Http/Controllers/Users/UsersController.php
, manipulate your UserRepository
as in the example bellow :
<?php
namespace App\Http\Controllers\Users;
use App\Http\Controllers\Controller;
use App\Repositories\Users\UserRepositoryInterface;
class UsersController extends Controller
{
/**BaseRepositoryInterface
* UsersController constructor.
*
* @param \App\Repositories\Users\UserRepositoryInterface $repository
*/
public function __construct(UserRepositoryInterface $repository)
{
parent::__construct();
$this->repository = $repository;
}
/**
* @param IndexUserRequest $request
*
* @return void
*/
public function index(IndexUserRequest $request)
{
// execute your repository custom methods
$this->repository->test();
// execute this package methods
$allStoredUsers = $this->repository->getAll();
}
See the protected properties that can be overridden in your own repositories in the BaseRepository.
See the available public methods in the BaseRepositoryInterface.
composer test
Please see CHANGELOG for more information on what has changed recently.
The MIT License (MIT). Please see License File for more information.