timwassenburg/laravel-service-generator

v1.0.5 2023-04-27 09:49 UTC

README

Logo

Latest Version on Packagist GitHub Tests Action Status Total Downloads License

目录

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

功能

此包添加了php artisan make:service {name}命令。该命令在app\Services中生成一个空的服务类,以便快速开始。

安装

使用composer安装此包。

composer require timwassenburg/laravel-service-generator --dev

使用

安装后,php artisan make:service {name}将在 artisan 命令列表中可用。

生成服务

要生成新的服务,请使用以下 artisan 命令。

php artisan make:service UserService

可选地,您可以通过添加多个方法名(用逗号分隔)来使用--methods参数。

php artisan make:service UserService --methods=register,login,logout

为模型生成服务

添加--service-S参数以生成模型的相应服务。

php artisan make:model Post --service

使用-a--all参数生成模型的服务、迁移、种子、工厂、策略和资源控制器。

php artisan make:model Post --all

为控制器生成服务

添加--service-S参数以生成控制器的服务。

php artisan make:controller PostController --service

服务模式

何时使用服务模式

一个常见问题是:我的业务逻辑放在哪里?您希望保持模型简洁,控制器函数轻量。有多种方法可以实现这一点,将业务逻辑提取到服务层是一个常见的方法。通过将您的业务逻辑封装在服务类中,您可以在控制器、命令、作业和中间件中重用逻辑。

如何使用服务

一旦创建了服务,就是添加业务逻辑的时候了。我们将讨论如何通过静态方法、依赖注入和使用接口和存储库来使用服务。

静态方法

使用服务的一个常见方法是静态地调用其方法。它类似于辅助函数。假设我们有一个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'));
    }
}

测试

使用以下命令运行测试:

composer test

更多生成包

寻找更多加快您工作流程的方法?请确保查看这些包。

上述提到的包是 Laravel Artisan Extender 的一部分。

贡献

贡献是开源社区如此美妙的学习、灵感和创作之地的原因。你做出的任何贡献都 非常感谢

如果你有使这个项目变得更好的建议,请 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)。有关更多信息,请参阅 许可文件