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);
    }
}
相关推荐
大白玉米18 分钟前
TVBOXOS6.0双端APP二开源码完整版全开源源码重构版
java·重构·php
布说在见4 小时前
踩坑与成长:WordPress、MyBatis-Plus 及前端依赖问题解决记录
服务器·学习·php
八月的雨季 最後的冰吻14 小时前
php算法-- 关联数组使用,优化sip账号去重
开发语言·php
rockmelodies1 天前
【PHP安全】免费解密支持:zend52、zend53、zend54好工具
开发语言·安全·php
速易达网络1 天前
PHP 与 Vue.js 结合的前后端分离架构
vue.js·php
Clownseven2 天前
使用 eBPF 实时捕获 TCP 重传告警:精准定位网络抖动问题
网络·tcp/ip·php
lsswear2 天前
PHP文件下载
php
行思理2 天前
如何使用 php-vulnerability-hunter
开发语言·php
hjc_0420432 天前
云效CI/CD教程(PHP项目)
ci/cd·php