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);
    }
}
相关推荐
dog2501 天前
圆锥曲线和二次曲线
开发语言·网络·人工智能·算法·php
千寻girling1 天前
五一劳动节快乐 [特殊字符][特殊字符][特殊字符]
java·c++·git·python·学习·github·php
xxjj998a1 天前
Laravel3.x:奠定现代PHP框架的重要里程碑
android·开发语言·php
xingpanvip1 天前
星盘接口开发文档:日运语料接口指南
android·开发语言·前端·css·php·lua
xxjj998a1 天前
Laravel 6.x 核心特性全解析
php·laravel
m0_738120721 天前
后渗透维权提权基础——CTF模拟红队进行权限维持(二)
前端·网络·windows·python·安全·php
NEGl DRYN1 天前
index.php 和 php
开发语言·php
玩代码的老秦1 天前
后端php连接SQL Server数据库报错解决方案
开发语言·数据库·php
梦梦代码精1 天前
LikeShop 是怎么解决数据库瓶颈的?
java·数据库·低代码·php
日取其半万世不竭2 天前
服务器自动备份方案:用 rsync + cron 实现异地增量备份
运维·服务器·php