mwakalingajohn/laravel-easy-repository
Composer 安装命令:
composer require mwakalingajohn/laravel-easy-repository
包简介
Simple repository pattern for laravel, with services!
README 文档
README
With easy repository, you can have the power of the repository pattern, without having to write too much code altogether. The package automatically binds the interfaces to the implementations, all you have to do is change in the configuration which implementation is being used at the moment!
Installation
You can install the package via composer:
composer require mwakalingajohn/laravel-easy-repository
Quick usage
This package overrides the default laravel php artisan make:model User command, and adds a few flags that can help you set up repository and service quickly.
// will genearate controller, factory, service, seeder, repository, resource and migration php artisan make:model User --all // use the service and repository flag to generate the class php artisan make:model User --service --repository // use the short form to generate model with service and repository php artisan make:model User -sr -rt
You can also create only the repository, or service, or both:
php artisan make:repository User // or php artisan make:repository UserRepository // or create together with a service php artisan make:repository User --service // or php artisan make:repository UserRepository --service // or create a service separately php artisan make:service User // or php artisan make:service UserService
The php artisan make:repository User will generate two files. One for the interface, and one for the repository class. The interface is bound to it's counter part class automatically depending on the current implementation being used. If the implementation for an interface is not provided, you can provide one manually or otherwise, attempting to use the service will bring up an error.
Eloquent is the default implementation. Other implementations will be added in the future. This is because the package was mainly to simplify usage of the repository pattern in laravel. The classes created are:
// app/Repositories/Interfaces/UserRepository.php <?php namespace App\Repositories\Interfaces; use Mwakalingajohn\LaravelEasyRepository\Repository; class UserRepositoryInterface extends Repository{ // Write something awesome :) }
and,
// app/Repositories/Eloquent/UserRepository.php <?php namespace App\Repositories\Eloquent; use Mwakalingajohn\LaravelEasyRepository\Repository; use Mwakalingajohn\LaravelEasyRepository\Implementations\Eloquent; use App\Repositories\Interfaces\UserRepositoryInterface; class UserRepository extends Eloquent implements UserRepositoryInterface{ /** * Model class to be used in this repository for the common methods inside Eloquent * @property Model|mixed $model; */ protected $model = Model::class; // Write something awesome :) }
also if you included the services flag, or created one by running a command, the service file generated is:
// app/Services/UserService <?php namespace App\Services; use Mwakalingajohn\LaravelEasyRepository\Service; class UserService extends Service{ /** * The repository interface to use in this service. Will allow to use within * methods $this->repository. It will be resolved from the container * @property string $repositoryInterface; */ protected string $repositoryInterface = "App\Repositories\Interfaces\UserRepositoryInterface"; // Define your custom methods :) }
The repository interface property, tells the service which repository to fetch from the container. And the repository once fetched will be available using
$this->repository variable.
Usage
In your controller you can use the service, or the repository.
<?php namespace App\Http\Controllers; use App\Services\UserService; use Illuminate\Http\Request; class UserController extends Controller { public function index() { $userService = new UserService; return $userService->all(); } } // or using the repository, the service container will automatically resolve the repository for you <?php namespace App\Http\Controllers; use App\Repositories\Interfaces\UserRepositoryInterface; use App\Services\UserService; use Illuminate\Http\Request; class UserController extends Controller { public $repository; public function __construct(UserRepositoryInterface $userRepositoryInterface) { $this->repository = $userRepositoryInterface; } public function index() { return $this->repository->all(); } }
The repository and service also comes in built with 5 common CRUD methods
interface Repository { /** * Fin an item by id * @param int $id * @return Model|null */ public function find(int $id); /** * Return all items * @return Collection|null */ public function all(); /** * Return query builder instance to perform more manouvers * @return Builder|null */ public function query(); /** * Create an item * @param array|mixed $data * @return Model|null */ public function create($data); /** * Update a model * @param int|mixed $id * @param array|mixed $data * @return bool|mixed */ public function update($id, array $data); /** * Delete a model * @param int|Model $id */ public function delete($id); }
You can publish the config file with:
php artisan vendor:publish --provider="Mwakalingajohn\LaravelEasyRepository\LaravelEasyRepositoryServiceProvider" --tag="easy-repository-config"
The configurations in the config file are standard, and can be extended with/depending on further requirements. No need to change any of the contents, unless you are very aware of what you are doing :) This is the contents of the published config file:
return [ /** * The directory for all the repositories */ "repository_directory" => "app/Repositories", /** * Default repository namespace */ "repository_namespace" => "App\Repositories", /** * The directory for all the services */ "service_directory" => "app/Services", /** * Default service namespace */ "service_namespace" => "App\Services", /** * Default repository implementation */ "default_repository_implementation" => "Eloquent", /** * Current repository implementation */ "current_repository_implementation" => "Eloquent", ];
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
mwakalingajohn/laravel-easy-repository 适用场景与选型建议
mwakalingajohn/laravel-easy-repository 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15.54k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2021 年 06 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「mwakalingajohn」 「laravel-easy-repository」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mwakalingajohn/laravel-easy-repository 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mwakalingajohn/laravel-easy-repository 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mwakalingajohn/laravel-easy-repository 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Alfabank REST API integration
Laravel package for Accurate Online API integration.
Shared RCX Laravel DataTables UI and configuration helpers.
Boot a Laravel project on any machine with one command: app:serve installs missing tools (PHP, Node, Composer, Herd, Docker), creates .env, sets up the database, runs migrations, builds assets, starts a queue worker and serves via Herd, Sail or artisan serve; app:down cleanly stops everything it sta
Branded, diagnostic error pages (500, 403, 404, 419, 503) for Filament — native Filament UI, dark mode and translations out of the box.
Turn any PDF into a Pingen-ready A4 letter (generated address cover page + A4 normalisation + safe margins) and send it through the Pingen print & mail API. Laravel-first.
统计信息
- 总下载量: 15.54k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-06-13