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);
    }
}
相关推荐
vx-程序开发9 小时前
【java项目分享】springboot农产品销售平台14952
java·javascript·spring boot·python·eclipse·django·php
城管不管9 小时前
rabbitmq如何保证消息不丢失?解决方案又是什么?
开发语言·ai·面试·职场和发展·rabbitmq·php·agent
JaguarJack11 小时前
PHP 引用计数机制深度解析
后端·php·服务端
凡尘——雨落凡尘1 天前
PHP 线上十大隐形故障复盘:90% 的网站卡顿、502、雪崩,都是这些细节导致的
redis·nginx·php·session
fengci.1 天前
Microweber CMS 未授权路径穿越漏洞(CVE-2026-65694)
android·开发语言·前端·学习·php
JSON_L1 天前
Fastadmin后台使用validate进行表单验证
php·fastadmin
用户4130294883461 天前
从 0 到 1 用 PHP 实现 Cron 表达式解析器(附完整源码)
php
JSON_L1 天前
Fastadmin关联查询报错 method not exist:think\db\Query->XXX
php·fastadmin
DFT计算杂谈2 天前
SCIENTIFIC REPORTS 非 van der Waals可调二维磁性材料设计平台
开发语言·php
凡尘——雨落凡尘2 天前
PHP实战复盘:高并发限流、防刷、验证码、会话安全、CSRF防护导致站点稳定性与安全加固实战
php·csrf 防护·csrf 攻击