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);
    }
}
相关推荐
JaguarJack18 小时前
PHP 的异步编程 该怎么选择
后端·php·服务端
BingoGo18 小时前
PHP 的异步编程 该怎么选择
后端·php
JaguarJack1 天前
为什么 PHP 闭包要加 static?
后端·php·服务端
ServBay2 天前
垃圾堆里编码?真的不要怪 PHP 不行
后端·php
用户962377954483 天前
CTF 伪协议
php
BingoGo5 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php
JaguarJack5 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php·服务端
BingoGo6 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php
JaguarJack6 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php·服务端
JaguarJack7 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
后端·php·服务端