Amqp控制器发布消息实例

2020-10-05  本文已影响0人  yichen_china
#控制器发布消息实例
<?php

declare (strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;

use Hyperf\Contract\ConfigInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Amqp\Producer;
use App\Amqp\Producer\DemoProducer;
use Hyperf\Utils\ApplicationContext;
use App\Amqp\Consumer\DemoConsumer;
class IndexController extends AbstractController
{
    use \Hyperf\Di\Aop\ProxyTrait;
    use \Hyperf\Di\Aop\PropertyHandlerTrait;
    function __construct()
    {
        if (method_exists(parent::class, '__construct')) {
            parent::__construct(...func_get_args());
        }
        self::__handlePropertyHandler(__CLASS__);
    }
    /**
     * @Inject()
     * @var ConfigInterface
     */
    private $config;
    // 通过 has(): bool 方法判断对应的 $key 值是否存在于配置中,$key 值可以通过 . 连接符定位到下级数组
    public function index()
    {
        $user = $this->request->all();
        $method = $this->request->getMethod();
        $message = new DemoProducer(json_encode($user));
        $producer = ApplicationContext::getContainer()->get(Producer::class);
        $result = $producer->produce($message);
        return [
            '$result' => $result,
            'message' => $message,
            'method' => $method,
            'msg' => $user,
        ];
    }
}
上一篇 下一篇

猜你喜欢

热点阅读