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);
    }
}
相关推荐
码农老李11 小时前
openEuler2403服务器版 原生官方镜像和飞腾定制镜像
开发语言·php
审判长烧鸡14 小时前
【PHPer转Go】fmt vs log/slog
go·php
HMS工业网络16 小时前
STP、RSTP到N-Ring的演进之路
服务器·开发语言·php
qq_5434478217 小时前
Tcping测速是什么?Tcping测速核心概念解析
服务器·网络·php
Mr数据杨20 小时前
AIGC工具平台-StoryBoard故事板
人工智能·aigc·php
IronMurphy20 小时前
Redis拷打第七讲(最终章)
数据库·redis·php
marsh02061 天前
49 openclaw故障排查:系统异常时的诊断方法
服务器·前端·青少年编程·ai·php·技术美术
暗冰ཏོ1 天前
PHP 全栈开发学习手册:从基础到高级实战、Laravel、Redis、面试题完整版
学习·php·laravel
dog2502 天前
为何新增网络路径反而引入额外时延
服务器·网络·php