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);
    }
}
相关推荐
禾小西1 小时前
06丨Redis 数据同步:主从库如何实现数据一致?
数据库·redis·php
Zooproxy全球IP代理2 小时前
海外代理IP纯净度评估:指标、检测与调度实践
网络协议·tcp/ip·php
Solis程序员2 小时前
Stripe 幂等表:把「不确定的重试」变成「查表返回」
前端·安全·bootstrap·php·agent
PHP实战开发录2 小时前
MySQL数字排序为什么乱
数据库·mysql·php
魔众6 小时前
ModStartBlog v11.3.0 安全加固、体验优化,自动化测试更强大
开源·php·laravel
郑州光合科技余经理6 小时前
本地生活平台搭建:跨业态用户标识怎么贯通
java·开发语言·前端·后端·uni-app·php·ai编程
枫叶丹48 小时前
语音 AI 怎样边听边答:实时对话系统的工作原理
开发语言·人工智能·chatgpt·开源·php·agent·codex
盛世宏博智慧档案8 小时前
Modbus TCP+PoE以太网温湿度传感器项目部署全流程,附网络调试实操
开发语言·tcp/ip·php·以太网·传感器·温湿度
优橙教育21 小时前
零基础学AI应用开发要多久?3个月能到什么水平
服务器·开发语言·网络·php
郭萌6961 天前
多商户平台的优惠券,真正的设计对象是「适用范围」而不是券模板
php