croudtech/laravel-repositories

Repositories layer for Laravel 5+

1.5.0 2017-04-06 16:14 UTC

This package is auto-updated.

Last update: 2022-06-08 00:33:06 UTC


README

68747470733a2f2f7472617669732d63692e6f72672f43726f7564537570706f72742f6c61726176656c2d7265706f7369746f726965732e7376673f6272616e63683d6d6173746572 68747470733a2f2f706f7365722e707567782e6f72672f63726f7564746563682f6c61726176656c2d7265706f7369746f726965732f762f737461626c65 68747470733a2f2f706f7365722e707567782e6f72672f63726f7564746563682f6c61726176656c2d7265706f7369746f726965732f762f756e737461626c65 68747470733a2f2f706f7365722e707567782e6f72672f63726f7564746563682f6c61726176656c2d7265706f7369746f726965732f636f6d706f7365726c6f636b 68747470733a2f2f706f7365722e707567782e6f72672f63726f7564746563682f6c61726176656c2d7265706f7369746f726965732f6c6963656e7365 68747470733a2f2f706f7365722e707567782e6f72672f63726f7564746563682f6c61726176656c2d7265706f7369746f726965732f646f776e6c6f616473

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;
  }
}