Hyperf 初体验-配置

本文介绍了Hyperf框架中配置的存储位置,包括`config.php`和`autoload`文件夹,以及如何通过依赖注入、注解和函数获取配置。特别提醒,注解方式获取时需注意单引号会导致错误。此外,文章还提及了使用环境变量存储敏感信息的方法。此外,作者还简单介绍了个人开发的极客返利平台。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Hyperf 的配置文件全部存放在 config\config.php、和 config\autoload 文件夹

config.php 与 autoload 文件夹内的配置文件的关系

config.phpautoload 文件夹内的配置文件在服务启动时都会被扫描并注入到 Hyperf\Contract\ConfigInterface 对应的对象中,配置的结构为一个键值对的大数组,两种配置形式不同的在于 autoload 内配置文件的文件名会作为第一层 键(Key) 存在,而 config.php 内的则以您定义的为第一层

获取配置

通过依赖注入获取配置
<?php

declare(strict_types=1);

namespace App\Controller;

use Hyperf\Contract\ConfigInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;

/**
 * @Controller()
 * Class AuthController
 * @package App\Controller
 */
class AuthController
{
    /**
     * @Inject()
     * @var ConfigInterface
     */
    private $config;

    /**
     * @GetMapping(path="index")
     * @param RequestInterface $request
     * @param ResponseInterface $response
     * @return mixed
     */
    public function index(RequestInterface $request, ResponseInterface $response)
    {
        //获取 config.php 里的内容
        $this->config->get('app_name','');

        // 获取 autoload/databases.php 里的配置
        $this->config->get('databases.default','');
    }
}

config.phpautoload 取得方式区别就是 autoload 里的内容需要加上文件名

通过注解 @Value() 获取配置
<?php

declare(strict_types=1);

namespace App\Controller;

use Hyperf\Config\Annotation\Value;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;

/**
 * @Controller()
 * Class AuthController
 * @package App\Controller
 */
class AuthController
{
    /**
     * @Value("databases.default.driver")
     */
    private $config;

    /**
     * @GetMapping(path="index")
     * @param RequestInterface $request
     * @param ResponseInterface $response
     * @return mixed
     */
    public function index(RequestInterface $request, ResponseInterface $response)
    {
        // 获取 autoload/databases.php 里的配置
        return $this->config;
    }
}
通过 config() 函数获取配置
<?php

declare(strict_types=1);

namespace App\Controller;

use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;

/**
 * @Controller()
 * Class AuthController
 * @package App\Controller
 */
class AuthController
{
    /**
     * @GetMapping(path="index")
     * @param RequestInterface $request
     * @param ResponseInterface $response
     * @return mixed
     */
    public function index(RequestInterface $request, ResponseInterface $response)
    {
        // 获取 autoload/databases.php 里的配置
        return config('databases.default.driver','');
    }
}

注意当使用 @Value() 注解获取配置文件时,记住是 双引号,而不是 单引号,单引号将获取不到内容,会报错。正确的用法如下:

 /**
 * @Value("databases.default.driver")
 */
private $config;

错误的用法:

 /**
 * @Value('databases.default.driver')
 */
private $config;

其实有一些敏感内容我们可以设置环境变量 将其保存在 .env 文件中.然后我们通过 env() 函数来获取值

env('APP_NAME', 'Hyperf Skeleton'),

关于配置文件就先说到这里,详细用法参考 官方文档

关于极客返利

极客返利 是由我个人开发的一款网课返利、返现平台。包含 极客时间返现、拉勾教育返现、掘金小册返现、GitChat返现。目前仅包含这几个平台。后续如果有需要可以考虑其他平台。 简而言之就是:你买课,我返现。让你花更少的钱,就可以买到课程。

https://geek.laravelcode.cn

https://geek.idaka.ink

版权许可
本作品采用 知识共享署名 4.0 国际许可协议 进行许可。

转载无需与我联系,但须注明出处,注明文章来源 Hyperf 初体验-配置

联系我
编程怪事
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值