scrumble-nl / laravel-csr
此包允许通过一条命令生成控制器、服务、仓库、模型和迁移
10.1.1
2024-03-15 06:04 UTC
Requires
- php: >=8.1
- laravel/framework: ^10.0|^11.0
Requires (Dev)
- phpunit/phpunit: ^9.6.0 || ^10.0.1
This package is auto-updated.
Last update: 2024-09-21 16:23:42 UTC
README
CSR代表控制器/服务/仓库。此包允许您通过单个命令设置所有这些,并提供选项以禁用三个层次中的任何一个,甚至可以自动生成模型和/或迁移。
使用方法
安装
使用composer安装此包
composer require scrumble-nl/laravel-csr
发布配置文件以设置默认类路径
php artisan vendor:publish --tag=laravel-csr
可以在此新建的csr.php
配置文件中修改默认类路径。
命令使用
基本命令是 php artisan csr:gen {name} {namespace (可选)}
。
这将一次性生成控制器、服务接口、服务、仓库接口和仓库。它们自动相互注入依赖,因此可以立即使用。太棒了!
最后,需要将生成的服务和/或仓库添加到AppServiceProvider.php
。
示例
php artisan csr:gen picture holiday
将生成以下文件
app/Http/Controllers/Holiday/PictureController.php
app/Interfaces/Services/Holiday/IPictureService.php
app/Services/Holiday/PictureService
app/Interfaces/Repositories/Holiday/IPictureRepository
app/Repositories/Holiday/PictureRepository
注意:命令将自动将第一个字符大写,因此 picture
将成为 Picture
。如果您希望类名像 PictureBook
,您必须正确输入。
现在需要注册服务和仓库
AppServiceProvider.php
<?php declare(strict_types=1); namespace App\Providers; use Illuminate\Support\ServiceProvider; use App\Services\Holiday\PictureService; use App\Repositories\Holiday\PictureRepository; use App\Interfaces\Services\Holiday\IPictureService; use App\Interfaces\Repositories\Holiday\IPictureRepository; class AppServiceProvider extends ServiceProvider { public function register(): void { app()->bind(IPictureService::class, PictureService::class); app()->bind(IPictureRepository::class, PictureRepository::class); } }
完成了!
示例输出
app/Http/Controllers/Holiday/PictureController.php
<?php declare(strict_types=1); namespace App\Http\Controllers\Holiday; use App\Http\Controllers\Controller; use App\Interfaces\Services\Holiday\IPictureService; class PictureController extends Controller { /** * @var IPictureService */ private $pictureService; /** * @param IPictureService $pictureService */ public function __construct(IPictureService $pictureService) { $this->pictureService = $pictureService; } }
命令选项
可以使用以下选项来禁用某些生成,甚至将模型和迁移添加到命令中
另请参阅:php artisan csr:gen --help
贡献
如果您想看到此包的添加/更改,您始终欢迎添加一些代码或改进它。
Scrumble
此产品最初由Scrumble为内部使用开发。由于我们使用了大量的开源包,我们希望回馈社区。我们希望这能帮助您像其他帮助过我们的人一样取得进步!