hyperf配置值獲取的三種方法


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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM