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);
    }
}
相关推荐
张鱼小丸子4 小时前
【无标题】云原生作业六
开发语言·php
哥坐11路18 小时前
网络IP跳动问题解决详
开发语言·php
一只哒布刘1 天前
第六次作业
开发语言·php
寰宇软件1 天前
PHP房屋出租出售高效预约系统小程序源码
前端·小程序·uni-app·vue·php
HUNAG-DA-PAO1 天前
Redis存在线程安全吗?为什么?
redis·安全·php
ianozo1 天前
BUU40 [安洵杯 2019]easy_serialize_php
android·开发语言·php
zgscwxd1 天前
php session数据存储位置选择
开发语言·php
ianozo2 天前
CTF 代码学习日记 PHP
java·学习·php
ontheway-xx2 天前
PHP+Apache+MySQL安装(Windows)
开发语言·php
网络安全queen3 天前
公证 网络安全
开发语言·php