raviyatechnical / laravel-service-generator
使用Laravel artisan命令生成服务
This package is auto-updated.
Last update: 2024-09-11 13:41:10 UTC
README
Laravel 服务生成器
快速为您的项目生成服务!
目录
特性
此包添加了php artisan make:service {name}命令。该命令在app\Services中生成一个空的服务类以开始使用。我主要为了自己的使用而创建这个,因为我喜欢能够从命令行生成重复的文件以保持我的工作流程一致。
安装
使用composer安装此包。
composer require raviyatechnical/laravel-service-generator
使用
安装后,php artisan make:service {name}将在artisan命令列表中可用。
生成服务
要生成新的服务,请使用以下artisan命令。
php artisan make:service UserService
为模型生成服务
添加--service或-S参数为模型生成服务。
php artisan make:model Post --service
使用-a或--all参数为模型生成服务、迁移、播种器、工厂、策略和资源控制器。
php artisan make:model Post --all
为控制器生成服务
添加--service或-S参数为控制器生成服务。
php artisan make:controller PostController --service
服务模式
何时使用服务模式
一个常见的问题是:我把我的业务逻辑放在哪里?你希望你的模型保持瘦小,控制器函数保持精简。有多种方法可以实现这一点,将你的业务逻辑提取到服务层是一个常见方法。通过将你的业务逻辑封装在服务类中,你可以在控制器、命令、作业和中间件中重用逻辑。
如何使用服务
一旦创建了服务,就是添加你的业务逻辑的时候了。我们将讨论如何通过静态方法、依赖注入使用服务,以及如何使用接口和存储库。
静态方法
使用服务的一个常见方法是调用其静态方法。它类似于辅助函数。假设我们有一个包含基于slug获取文章的方法的PostService。
namespace App\Services; use App\Models\Post; class PostService { // Declare the function as static public static function getPostBySlug(string $slug): Post { return Post::with('tags') ->where('slug', $slug) ->get(); } }
然后你可以将服务类包含在你的控制器中,例如,并静态调用getPostBySlug方法。
namespace App\Http\Controllers; // Include the service use App\Services\PostService; class PostController extends Controller { public function show(string $slug) { // Call the method statically from the service class $post = PostService::getPostBySlug($slug); return view('posts.show', compact('post')); } }#
getPostBySlug方法在这个例子中是一个非常简单的函数,但正如你所看到的,它使你的控制器保持瘦小,并将业务逻辑分离。请注意,静态类和方法是无状态的。该类不会在自身中保存任何数据。
依赖注入
另一种流行的方法是使用依赖注入来使用服务。通过依赖注入,你可以编写松耦合的代码。当正确完成时,这将提高代码的灵活性和可维护性。
我们之前用作示例的PostService将几乎保持不变,除了我们不再将函数声明为类的静态函数。
namespace App\Services; use App\Models\Post; class PostService { public function getPostBySlug(string $slug): Post { return Post::with('tags') ->where('slug', $slug) ->get(); } }
接下来,我们将服务注入到我们想要使用它的类的构造函数中。在构造函数内部,我们将对象分配给 $postService 类属性。现在,$postService 属性将在类中的所有函数中可调用,通过 $this->postService。当你输入时,你的IDE已经会为你的 PostService 类中的函数提供类型提示,在这个例子中只有 ->getPostBySlug($slug)。
namespace App\Http\Controllers; // Include the service use App\Services\PostService; class PostController extends Controller { // Declare the property protected $postService; // Inject the service into the constructor public function __construct(PostService $postService) { // Assign the service instance to the class property $this->postService = $postService; } public function show($slug) { // Call the method you need from the service via the class property $post = $this->postService->getPostBySlug($slug); return view('posts.show', compact('post')); } }
贡献
贡献使开源社区成为一个如此美妙的学习、灵感和创造的地方。你做出的任何贡献都将被 高度赞赏。
如果你有改进这个项目的建议,请Fork该项目并创建一个Pull Request。你也可以简单地创建一个带有“enhancement”标签的问题。别忘了给这个项目加星!再次感谢!
- Fork项目
- 创建你的特性分支(
git checkout -b feature/AmazingFeature) - 提交你的更改(
git commit -m '添加一些AmazingFeature') - 推送到分支(
git push origin feature/AmazingFeature) - 打开Pull Request
第一个灵感包
https://github.com/timwassenburg/laravel-service-generator
许可
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。