hassanejazpvt / laravel-csr
该包使得通过一个命令即可生成控制器、服务、仓库、模型和迁移文件
1.0.0
2022-10-15 17:08 UTC
Requires
- php: >=7.3
- laravel/framework: ^8.0
Requires (Dev)
- phpunit/phpunit: ~9.3
This package is auto-updated.
Last update: 2024-09-15 21:16:28 UTC
README
CSR代表控制器/服务/仓库。此包允许您通过单个命令设置所有这些,还可以选择禁用三层中的任意一层,甚至可以自动生成模型和/或迁移。
用法
安装
使用composer安装包
composer require hassanejazpvt/laravel-csr
发布配置文件以设置默认类路径
php artisan vendor:publish --tag=laravel-csr
可以在此新创建的 csr.php 配置文件中修改默认类路径。
命令用法
基本命令是 php artisan csr:gen {名称} {命名空间(可选)}。
这将一次性生成控制器、服务接口、服务、仓库接口和仓库。它们会自动相互注入依赖,因此可以立即使用。太棒了!
最后,需要将生成的服务和/或仓库添加到您的 AppServiceProvider.php。
示例
php artisan csr:gen picture holiday 将生成以下文件
app/Http/Controllers/Holiday/PictureController.phpapp/Interfaces/Services/Holiday/IPictureService.phpapp/Services/Holiday/PictureServiceapp/Interfaces/Repositories/Holiday/IPictureRepositoryapp/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 为内部使用开发。因为我们使用了很多开源包,我们希望回馈社区。我们希望这能帮助您取得和其他人帮助我们的同样多的进展!