9、Hyperf 3 快速使用 - 依赖注入注解 Service 层使用

作者: 温新

分类: 【Hyperf 3 基础系列】

阅读: 516

时间: 2023-04-25 12:21:26

hi,我是温新,一名 PHPer

Hypref 版本:Hyperf 3.0

学习目标:消息依赖注入,掌握注解 #[Inject] 的使用

本篇文章学习一下依赖注入,在开发时经常使用。

在 MVC 中,我们再添加 Service 层,专门用于处理业务逻辑。在 Hyperf 中,我们需要把它注入到中容器对中,方便调用。这也是本篇文章的重要目的。

方式一:通过构造函数注入

第一步:添加 UserService.php

<?php
// App\Service\UserService.php
    
namespace App\Service;

class UserService
{
    public function getInfoById(int $id)
    {
        return 'user service';  
    }
}

第二步:构造函数中注入

<?php

// App\Controller\IndexController.php

use App\Service\UserService;

// 构造函数注入
public function __construct(protected UserService $userService)
{
}

public function index()
{
    // 使用 UserService 层中的文法
    return $this->userService->getInfoById(1);
}

第三步:访问 URL:curl http://localhost:9501/

方式二:通过 #[Inject] 注解使用 Service

删除构造函数,添加如下注解代码,即可完成注入。

<?php
// App\Controller\IndexController.php
    
// 使用注解形式注入
#[Inject]
private UserService $userService;

public function index()
{
    return $this->userService->getInfoById(1);
}

方式三:通过抽象对象注入

上面的代码中,只使用了 UserService.php 层,从合理性角度上说,Controller 中不应该直接面向 UserService.php 类,可能更多的是 UserServiceInterface.php 接口类,此时需要在 dependencies.php 配置文件中绑定他们两的关系。

关于接口实现的方式,在实际开发中就多了一个步骤。这个看要求而行吧。

第一步:创建 UserServiceInterface.php

<?php
// App\ServerInterface\UserServiceInterface.php
    
namespace App\ServerInterface;

interface UserServiceInterface
{
    public function getInfoById(int $id);
}

第二步:UserService 实现该接口

<?php
// App\Service\UserService.php

namespace App\Service;

use App\ServerInterface\UserServiceInterface;

class UserService implements UserServiceInterface
{
    public function getInfoById(int $id)
    {
        return 'user service';  
    }
}

第三步:dependencies.php 中绑定关系配置

<?php
// config\autoload\dependencies.php
    
declare(strict_types=1);

return [
    \App\ServerInterface\UserServiceInterface::class => \App\Service\UserService::class
];

第四步:控制器中注入

<?php
// App\Controller\IndexController.php
    
use App\ServerInterface\UserServiceInterface;

/**
* 注意注意的变化
*
* @var UserServiceInterface
*/
#[Inject]
private $userService;

public function index()
{
    return $this->userService->getInfoById(1);=
}

方式四:通过工厂对象注入

第一步:创建一个工厂用于生成 UserService 对象

<?php
// App\ServiceFactory\UserServiceFactory.php
    
namespace App\ServiceFactory;

use App\Service\UserService;
use Hyperf\Contract\ConfigInterface;
use Psr\Container\ContainerInterface;

class UserServiceFactory
{
    // 实例化容器对象
    public function __invoke(ContainerInterface $container)
    {
        // 获取容器中的 config 对象
        $config = $container->get(ConfigInterface::class);
        // 我们假设对应的配置的 key 为 cache.enable
        $enableCache = $config->get('cache.enable', true);
        // 生成 UserService 实例对象
        return make(UserService::class, compact('enableCache'));
    }
}

第二步:UserService 构造函数添加一个参数用于接收对应的值

<?php
// App\Service\UserService.PHP

public function __construct(private bool $enableCache)
{
}

第三步:绑定工厂与服务层的关系

<?php
// config\autoload\dependencies.php
    
declare(strict_types=1);

return [
    \App\ServerInterface\UserServiceInterface::class => \App\ServiceFactory\UserServiceFactory::class
];

第四步:控制器调用

<?php
// App\Controller\IndexController.php
        
/**
 * @var UserServiceInterface
 */
 #[Inject]
 private $userService;

public function index()
{
    return $this->userService->getInfoById(1);
}

我是温新,本篇文章结束啦。

请登录后再评论