CroudSupport / laravel-repositories by jscrobinson

Repositories layer for Laravel 5+
5,045
2
8
Package Data
Maintainer Username: jscrobinson
Maintainer Contact: jscrobinson@gmail.com (Jim Robinson)
Package Create Date: 2017-03-07
Package Last Update: 2020-01-07
Language: PHP
License: BSD-4-Clause
Last Refreshed: 2024-11-19 03:21:34
Package Statistics
Total Downloads: 5,045
Monthly Downloads: 1
Daily Downloads: 0
Total Stars: 2
Total Watchers: 8
Total Forks: 1
Total Open Issues: 0

laravel-repositories

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