This package provides artisan commands to easily generate Services and Repositories, following the Service-Repository Pattern, which are not natively supported in Laravel. It helps developers maintain a clean and scalable code structure.
The code is designed so that any developer can build upon it, as it follows best practices and clean code principles.
- Generate repository and service classes with a single command.
- Adheres to the Service-Repository Pattern.
- Automatically binds repositories to interfaces in the Laravel service container.
- Customizable templates for your own conventions.
- PHP 8.0 - 8.4
- Composer
- nette/php-generator >= 4.1
https://packagist.org/packages/josanangel/service-repository-manager
composer require josanangel/service-repository-manager
<?php
return [
...,
josanangel\ServiceRepositoryManager\ServiceRepositoryManagerServiceProvider::class,
];
# service repository manager .env
SRM_MODULES=false
#SRM_REPOSITORIES_NAMESPACE='App\Modules\{module_name}\Repositories'
SRM_REPOSITORIES_NAMESPACE='App\Repositories'
SRM_REPOSITORIES_PATH='Repositories'
SRM_REPOSITORIES_SUFFIX='Repository'
#SRM_SERVICES_NAMESPACE='App\Modules\{module_name}\Services'
SRM_SERVICES_NAMESPACE='App\Services'
SRM_SERVICES_PATH='Services'
SRM_SERVICES_SUFFIX='Service'
php artisan vendor:publish --tag=service_repository_manager_config
If your project follows a module structure such as https://nwidart.com/laravel-modules/v6/introduction
You need to change SRM_MODULES
to true and set a repository and service namespace which contains
the {node_modules}
keyword to replace in execution time when commands are called.
Then the commands will need --module=
option to specify where file should be located.
Example .env using module:
SRM_MODULES=true
SRM_REPOSITORIES_NAMESPACE='App\Modules\{module_name}\Repositories'
SRM_SERVICES_NAMESPACE='App\Modules\{module_name}\Services'
Commands examples using module:
php artisan make:repository User --module=User
php artisan make:service User --module=User
composer dump-auto
Command | Parameter | Type | Description | Required |
---|---|---|---|---|
php artisan make:repository |
name |
Argument | Name of the repository you want to create. | Yes |
--module |
Option | Specifies where the file(s) will be generated. | No | |
--crud |
Option | Specifies if the repository should include predefined CRUD functions. | No | |
php artisan make:service |
name |
Argument | Name of the service you want to create. | Yes |
--module |
Option | Specifies where the file(s) will be generated. | No | |
--repositories |
Option | Indicates possible repositories to declare as injected dependencies in the main service file. | No | |
--services |
Option | Indicates possible services to declare as injected dependencies in the main service file. | No | |
--repositories-crud |
Option | Indicates repositories to declare as injected dependencies, including predefined CRUD functions. | No |
php artisan make:repository User
<?php
namespace App\Repositories;
class UserRepository
{
public function __construct()
{
}
}
php artisan make:repository User --crud
<?php
namespace App\Repositories;
class UserRepository
{
protected $model;
/**
* TODO: initialize $this->model to your model in order to repository works successfully
* ... e.g.: $this->model = User::class;
*/
public function __construct()
{
}
public function all()
{
return $this->model->all();
}
public function create($data)
{
return $this->model->create($data);
}
public function find($id)
{
return $this->model->find($id);
}
public function update($id, $data)
{
return $this->model->where("id",$id)->update($data);
}
public function delete($id)
{
return $this->model->where("id",$id)->delete();
}
}
php artisan make:service User
<?php
namespace App\Services;
class UserService
{
public function __construct()
{
}
}
php artisan make:service User --services=auth
<?php
namespace App\Services;
class UserService
{
protected AuthService $authService;
public function __construct(AuthService $authService)
{
$this->authService = $authService;
}
}
<?php
namespace App\Services;
class AuthService
{
public function __construct()
{
}
}
php artisan make:service User --services=auth,map --repositories=auth
<?php
namespace App\Repositories;
class AuthRepository
{
public function __construct()
{
}
}
<?php
namespace App\Services;
class AuthService
{
public function __construct()
{
}
}
<?php
namespace App\Services;
class MapService
{
public function __construct()
{
}
}
<?php
namespace App\Services;
use App\Repositories\AuthRepository;
class UserService
{
protected AuthRepository $authRepository;
protected AuthService $authService;
protected MapService $mapService;
public function __construct(AuthRepository $authRepository, AuthService $authService, MapService $mapService)
{
$this->authRepository = $authRepository;
$this->authService = $authService;
$this->mapService = $mapService;
}
}
php artisan make:service User --repositories-crud=User
<?php
namespace App\Repositories;
class UserRepository
{
protected $model;
public function __construct()
{
}
public function index()
{
$this->model->all();
}
public function store($data)
{
$this->model->create($data);
}
public function show($id)
{
$this->model->findById($id);
}
public function update($id, $data)
{
$this->model->where("id",$id)->update($data);
}
public function destroy($id)
{
$this->model->where("id",$id)->delete();
}
}
<?php
namespace App\Services;
use App\Repositories\UserRepository;
class UserService
{
protected UserRepository $userRepository;
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
}