croudtech/laravel-repositories

Repositories layer for Laravel 5+

1.5.0 2017-04-06 16:14 UTC

This package is auto-updated.

Last update: 2024-11-08 05:59:46 UTC


README

Use Repositories with the Laravel framework

Setup

This package uses dependency injection via the Laravel container to inject repositories into controllers or other classes that may require them.

Register Service Provider

Add service provider to the 'providers' section of your app config.

'providers' => [
    ...
    CroudTech\Repositories\Providers\RepositoryServiceProvider::class,
    ...
],

Add the repository definitions into your resources config file as follows:

<?php
use \CroudTech\Repositories\TestApp\Repositories\Contracts\UserRepositoryContract;
use \CroudTech\Repositories\TestApp\Repositories\UserRepository;
use \CroudTech\Repositories\TestApp\Repositories\UserApiRepository;
use \CroudTech\Repositories\TestApp\Transformers\UserTransformer;
use \CroudTech\Repositories\TestApp\Controllers\UserController;
use \CroudTech\Repositories\TestApp\Controllers\UserApiController;

return [
    'repositories' => [
        UserRepositoryContract::class => UserRepository::class,
    ],

    'repository_transformers' => [
        UserRepository::class => UserTransformer::class,
        UserApiRepository::class => UserTransformer::class,
    ],

    'contextual_repositories' => [
        UserController::class => UserRepositoryContract::class,
        UserApiController::class => UserApiRepository::class,
    ],
];

The 'repositories' defined the concrete implementation of each repository contract.

The 'repository_transformers' defines the transformer classes that should be injected into each repository.

The 'contextual_repositories' Defines contextual repositories. This is useful where different controllers will need different implementations of the same contract.

To specify the repository required by a controller just add it's contract or classname into the construct method of the controller.

For example to inject the UserRespsitory into a UserController:

<?php
namespace App\Controllers;

use \CroudTech\Repositories\TestApp\Repositories\Contracts\UserRepositoryContract;

class UserController extends Controller
{
  protected $repository;

  public function __construct(UserRepositoryContract $user_repository)
  {
    $this->repository = $user_repository;
  }
}