1  通过config对象的方式获取

注意 use Inject 和 ConfigInterface

<?php

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

namespace App\Controller;

use Hyperf\Di\Annotation\Inject;
use Hyperf\Contract\ConfigInterface;

class IndexController extends AbstractController
{

    /**
     * @Inject
     * @var ConfigInterface
     */
    protected $config;


    public function index()
    {
        var_dump($this->config->get('redis' ));//此时打印出array

    }

}

2 通过 Value 获取

2.1 注意 use Value类  

2.2 @Value("databases.default.driver")的引号是双引号

 

<?php

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

namespace App\Controller;

use Hyperf\Config\Annotation\Value;

class IndexController extends AbstractController
{


    /**
     * @Value("databases.default.driver")
     */
    private $configValue;


    public function index()
    {
       var_dump($this->configValue);//此时打印出mysql
   }

}

3 通过助手函数获取 

config(string $key, $default)

相关文章:

  • 2022-12-23
  • 2022-01-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-07
  • 2022-12-23
  • 2021-11-30
猜你喜欢
  • 2021-11-27
  • 2022-12-23
  • 2021-04-06
  • 2021-10-31
  • 2021-12-31
  • 2021-11-14
  • 2022-12-23
相关资源
相似解决方案