veseluy-rodjer/laravel-service-generator

v1.0.1 2021-10-25 16:56 UTC

This package is auto-updated.

Last update: 2024-09-25 08:25:53 UTC


README


Logo

Laravel 服务生成器

快速为您的项目生成服务!



目录

  1. 功能
  2. 安装
  3. 用法
  4. 服务模式
  5. 更多生成器包
  6. 贡献
  7. 许可

功能

此包添加了php artisan make:service {name}命令。该命令在app\Services中生成一个空的服务类以开始。我主要为了自己的使用而制作这个,因为我喜欢能够从命令行生成重复的文件以保持我的工作流程一致。

安装

使用composer安装此包。

composer require timwassenburg/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”来打开一个问题。别忘了给这个项目加星!再次感谢!

  1. Fork 项目
  2. 创建您的功能分支(git checkout -b feature/AmazingFeature
  3. 提交您的更改(git commit -m '添加一些AmazingFeature'
  4. 推送到分支(git push origin feature/AmazingFeature
  5. 打开Pull Request

许可

MIT 许可证(MIT)。请参阅许可证文件获取更多信息。