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);
    }
}
相关推荐
Sagittarius_A*4 小时前
【RCELABS】Level 17~18 —— PHP命令执行函数与环境变量注入
开发语言·安全·web安全·靶场·php·rce
破碎的南瓜4 小时前
靶场中使用到的php函数以及每种漏洞的防御方式
开发语言·php
谁看我谁 狼家二丫5 小时前
局域网文件共享实战:从“账户被禁用”到成功互传文件
开发语言·php
AC赳赳老秦6 小时前
软著公开信息批量采集:OpenClaw 抓取软件著作权公开数据,分析企业技术布局方向
大数据·网络·人工智能·python·php·deepseek·openclaw
梓仁沐白6 小时前
【Agent 设计模式】多智能体协作
microsoft·设计模式·php
开源推荐官6 小时前
刚上架的商品搜不到,tigshop开源商城搜索是怎么查的
mysql·开源·php
Dxy12393102168 小时前
TCP三次握手和四次挥手的过程详解
网络·tcp/ip·php
weixin_BYSJ19871 天前
springboot校园自习室管理小程序---附源码32142
java·javascript·spring boot·python·django·flask·php
大鱼>1 天前
eBPF内核编程:从TC到XDP的全栈可观测性
linux·服务器·php
aixingpan1 天前
aixingpan.cn API开发文档:api_docs_trichart_natal_solararc_transit2接口指南
前端·php