2017 © Pedro Peláez
 

library laravel-base-repository

An abstract base repository with predefined common features.

image

okipa/laravel-base-repository

An abstract base repository with predefined common features.

  • Thursday, July 5, 2018
  • by Okipa
  • Repository
  • 1 Watchers
  • 1 Stars
  • 40 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 18 Versions
  • 300 % Grown

The README.md

laravel-base-repository

An abstract base repository with predefined common features., (*1)

Source Code Latest Version Total Downloads License: MIT Build Status Code Coverage Scrutinizer Code Quality, (*2)


Before starting

The repository pattern has several objectives : - Encourage development good practices (separation of concerns, code reusability, ...) - Improve code testability, (*3)

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., (*4)

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., (*5)


Installation

The repository pattern setup is not complicated but requires several steps to be accomplished.
Follow them one by one :, (*6)

  • Install the package with composer :
composer require okipa/laravel-base-repository
  • Create a app/Repositories directory where you will store your different project repositories., (*7)

  • Create your app/Repositories/BaseRepositoryInterface.php interface and your app/Repositories/BaseRepository.php abstract class :, (*8)

<?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 associated UserRepository :
<?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 the config/app.php :
// ...

'providers' => [
    // other provider declarations ...

    // custom providers
    App\Providers\RepositoryServiceProvider::class,
],

// ...
  • Add a $repository attribute to your app/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., (*9)


Usage

In your app/Http/Controllers/Users/UsersController.php, manipulate your UserRepository as in the example bellow :, (*10)

<?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();
    }

API

Properties

See the protected properties that can be overridden in your own repositories in the BaseRepository., (*11)

Public methods

See the available public methods in the BaseRepositoryInterface., (*12)


Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently., (*13)


Contributors


License

The MIT License (MIT). Please see License File for more information., (*14)

The Versions

05/07 2018

dev-master

9999999-dev https://github.com/Okipa/laravel-base-repository

An abstract base repository with predefined common features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arthur Lorent

laravel repository php base pattern package okipa

05/07 2018

1.1.6

1.1.6.0 https://github.com/Okipa/laravel-base-repository

An abstract base repository with predefined common features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arthur Lorent

laravel repository php base pattern package okipa

05/07 2018

1.1.5

1.1.5.0 https://github.com/Okipa/laravel-base-repository

An abstract base repository with predefined common features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arthur Lorent

laravel repository php base pattern package okipa

05/07 2018

1.1.4

1.1.4.0 https://github.com/Okipa/laravel-base-repository

An abstract base repository with predefined common features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arthur Lorent

laravel repository php base pattern package okipa

03/07 2018

1.1.3

1.1.3.0 https://github.com/Okipa/laravel-base-repository

An abstract base repository with predefined common features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arthur Lorent

laravel repository php base pattern package okipa

03/07 2018

1.1.2

1.1.2.0 https://github.com/Okipa/laravel-base-repository

An abstract base repository with predefined common features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arthur Lorent

laravel repository php base pattern package okipa

03/07 2018

1.1.1

1.1.1.0 https://github.com/Okipa/laravel-base-repository

An abstract base repository with predefined common features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arthur Lorent

laravel repository php base pattern package okipa

03/07 2018

1.1.0

1.1.0.0 https://github.com/Okipa/laravel-base-repository

An abstract base repository with predefined common features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arthur Lorent

laravel repository php base pattern package okipa

20/06 2018

1.0.9

1.0.9.0 https://github.com/Okipa/laravel-base-repository

An abstract base repository with predefined common features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arthur Lorent

laravel repository php base pattern package okipa

20/06 2018

1.0.8

1.0.8.0 https://github.com/Okipa/laravel-base-repository

An abstract base repository with predefined common features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arthur Lorent

laravel repository php base pattern package okipa

08/06 2018

1.0.7

1.0.7.0 https://github.com/Okipa/laravel-base-repository

An abstract base repository with predefined common features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arthur Lorent

laravel repository php base pattern package okipa

30/05 2018

1.0.6

1.0.6.0 https://github.com/Okipa/laravel-base-repository

An abstract base repository with predefined common features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arthur Lorent

laravel repository php base pattern package okipa

30/05 2018

1.0.5

1.0.5.0 https://github.com/Okipa/laravel-base-repository

An abstract base repository with predefined common features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arthur Lorent

laravel repository php base pattern package okipa

30/05 2018

1.0.4

1.0.4.0 https://github.com/Okipa/laravel-base-repository

An abstract base repository with predefined common features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arthur Lorent

laravel repository php base pattern package okipa

30/05 2018

1.0.3

1.0.3.0 https://github.com/Okipa/laravel-base-repository

An abstract base repository with predefined common features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arthur Lorent

laravel repository php base pattern package okipa

18/05 2018

1.0.2

1.0.2.0 https://github.com/Okipa/laravel-base-repository

An abstract base repository with predefined common features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arthur Lorent

laravel repository php base pattern package okipa

14/05 2018

1.0.1

1.0.1.0 https://github.com/Okipa/laravel-base-repository

An abstract base repository with predefined common features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arthur Lorent

laravel repository php base pattern package okipa

14/05 2018

1.0.0

1.0.0.0 https://github.com/Okipa/laravel-base-repository

An abstract base repository with predefined common features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arthur Lorent

laravel repository php base pattern package okipa