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 小时前
stp,rstp,mstp的区别
服务器·网络·php
一念&6 小时前
每日一个网络知识点:网络层NAT
服务器·网络·php
LXMXHJ8 小时前
php开发
开发语言·php
Jtti12 小时前
SSH连接服务器超时?可能原因与解决方案
服务器·网络·php
小糖学代码14 小时前
网络:3.Socket编程TCP
网络·tcp/ip·php
BingoGo17 小时前
PHP8.5 的新 URI 扩展
后端·php
JaguarJack19 小时前
PHP8.5 的新 URI 扩展
后端·php
、花无将1 天前
PHP:配置问题从而导致代码运行出现错误
开发语言·php
zorro_z1 天前
ThinkPHP8学习篇(九):模型(一)
php
打酱油的;2 天前
【无标题】
爬虫·python·php