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 小时前
Redis 主从复制内核深潜:从物理模型到全量增量同步的本质
网络·redis·php
三84411 小时前
PHP 污点分析绕过实战:CURL、get_meta_tags 与 fpm_get_status 引入非预定义污点源
开发语言·php
辻弋20113 小时前
一键优化电源计划、游戏模式、独显强制、禁用后台录制——DeltaForceBooster专治三角洲行动卡顿掉帧,所有改动可一键还原
服务器·数据库·windows·游戏·电脑·php
Sagittarius_A*14 小时前
【好靶场】PHP反序列化入门练习
安全·php·web·反序列化
2601_9657984714 小时前
How to Build a Scalable Classified Directory with Classima Theme
php·theme
何以解忧,唯有..14 小时前
Python logging 模块:从入门到精通
python·microsoft·php
quantdash_cc14 小时前
历史数据断层与REST请求慢到崩溃?QuantDash高性能量化数据API终极解决方案
开发语言·python·缓存·php·量化·quantdash
sbjdhjd15 小时前
CTF 技术复盘:从参数类型绕过到正则回溯 | Merry Christmas PHP CTF(gift.php & gift_plus.php)
安全·网络安全·云计算·php·ctf·红队·网络攻防
Sagittarius_A*15 小时前
【好靶场】PHP反序列化入门练习2
开发语言·web安全·信息安全·php·代码审计·反序列化
PHP实战开发录16 小时前
微信小程序多环境配置混用排查记录
微信小程序·php·开发·接口隔离原则