Hyperf 初体验-配置

作者: hedeqiang

发布时间: 2019-10-17 23:55:11

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 初体验-配置