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);
    }
}
相关推荐
BingoGo1 天前
PHP 泛型之殇 泛型 RFC 提案被拒绝
后端·php
JaguarJack1 天前
PHP 泛型之殇 泛型 RFC 提案被拒绝
后端·php
用户3074596982072 天前
PHP 扩展——从入门到理解
php
鹏仔先生3 天前
拷贝漫画APP下载页PHP程序,后台带免费AI写作
php
云水一下3 天前
从零开始学 PHP 系列(一):PHP 的前世今生与开发环境搭建
开发语言·php
xingpanvip3 天前
星盘接口开发文档:本命盘接口指南
android·开发语言·css·php·lua
酉鬼女又兒3 天前
零基础入门计算机网络运输层:端到端通信核心作用、端口号分类规则、复用分用工作机制及UDP与TCP协议全方位对比详解
网络·网络协议·tcp/ip·计算机网络·考研·udp·php
dog2503 天前
不要再继续优化 TCP
网络协议·tcp/ip·php
Channing Lewis3 天前
PHP 解析 Excel 的那些坑:一次“行号错位”引发的数据丢失
开发语言·php·excel
Cheng小攸3 天前
渗透行为分析与检测
开发语言·php