zoukankan      html  css  js  c++  java
  • 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 AppController;
    
    use HyperfDiAnnotationInject;
    use HyperfContractConfigInterface;
    
    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 AppController;
    
    use HyperfConfigAnnotationValue;
    
    class IndexController extends AbstractController
    {
    
    
        /**
         * @Value("databases.default.driver")
         */
        private $configValue;
    
    
        public function index()
        {
           var_dump($this->configValue);//此时打印出mysql
       }
    
    }

    3 通过助手函数获取 

    config(string $key, $default)

  • 相关阅读:
    super返回不过来
    对象与内存控制
    java数组学习
    careercup-递归和动态规划 9.8
    careercup-递归和动态规划 9.7
    careercup-递归和动态规划 9.6
    careercup-递归和动态规划 9.5
    careercup-递归和动态规划 9.4
    careercup-递归和动态规划 9.3
    careercup-递归和动态规划 9.2
  • 原文地址:https://www.cnblogs.com/aln0825/p/13967286.html
Copyright © 2011-2022 走看看