7. 配置

三种获取配置的方法

返回 /config/config.php 、/config/autoload/xxx.php 中的值

php 复制代码
<?php
namespace App\Controller;

use Hyperf\Config\Annotation\Value;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use function Hyperf\Config\config;

#[AutoController]
class ConfigController
{
  	// 1. 通过 注入 Hyperf\Contract\ConfigInterface 接口类来获取
    #[Inject]
    private ConfigInterface $config;

    // 2. 通过使用 Value 注解注入 对应配置项
    #[Value('foo.bar')]
    private $bar;

    public function index()
    {
        return $this->config->get('foo.bar');
    }
  
    public function value()
    {
        return $this->bar;
    }
  
    // 3. 通过 Config 全局函数
    public function config()
    {
      return config('foo.bar', 123);
    }
}
相关推荐
南梦浅3 小时前
全过程步骤(从零到高可用企业网络)
开发语言·网络·php
xiangpanf3 小时前
Laravel 9.X新特性全解析
php·laravel
xiangpanf4 小时前
Laravel与ThinkPHP框架深度对比
php·laravel
hongtianzai4 小时前
Laravel7.x十大核心特性解析
java·c语言·开发语言·golang·php
ZHOUPUYU4 小时前
从缓存到消息队列的全面应用,PHP与Redis深度实战
redis·缓存·php
ZHOUPUYU5 小时前
PHP性能分析与调优:从定位瓶颈到实战优化
开发语言·后端·html·php
Du_chong_huan5 小时前
1.6 面对攻击的网络 | 《计算机网络:自顶向下方法》精读版
网络·安全·php
hongtianzai5 小时前
Laravel 10.x重磅更新:全新特性速览
php·laravel
hongtianzai5 小时前
Laravel8.x核心特性全解析
java·c语言·开发语言·golang·php
hongtianzai5 小时前
Laravel6.x重磅发布:LTS版本新特性全解析
c语言·开发语言·php·laravel