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);
    }
}
相关推荐
2601_9637713718 小时前
Optimizing Logistics and Transportation Sites on WordPress
前端·数据库·windows·php
草邦设计开发团队_媒体资源平台19 小时前
Linux 稳定常驻启动 PHP 队列,服务器重启自动恢复
linux·服务器·php·队列执行
2601_9637713719 小时前
Speeding Up Creative Agency Portfolios: A Performance Guide
前端·数据库·php
七夜zippoe20 小时前
OpenClaw 数据加密:敏感信息保护的完整方案
数据库·mysql·php·openclaw·敏感保护
烤代码的吐司君2 天前
Redis IO 多路复用原理与引入原因深度解析
数据库·redis·php
鹏易灵2 天前
C++——8.移动语义初探(移动构造、移动赋值)
开发语言·c++·php
从零开始的代码生活_2 天前
Linux epoll 多路转接详解
linux·运维·网络·后端·tcp/ip·计算机网络·php
不吃辣4902 天前
前端应用的离线暂停更新策略:构建稳定可靠的渐进式更新方案
开发语言·前端·php
钟智强3 天前
我警告了 329 天
git·php·wordpress·webshell
从零开始的代码生活_3 天前
Linux 五种 IO 模型详解
linux·服务器·数据库·c++·后端·php