前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Hyperf 初体验-配置

Hyperf 初体验-配置

作者头像
hedeqiang
发布2019-12-17 21:25:05
1.1K0
发布2019-12-17 21:25:05
举报
文章被收录于专栏:LaravelCodeLaravelCode

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

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

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

获取配置

通过依赖注入获取配置
代码语言:javascript
复制
<?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() 获取配置
代码语言:javascript
复制
<?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() 函数获取配置
代码语言:javascript
复制
<?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() 注解获取配置文件时,记住是 双引号,而不是 单引号,单引号将获取不到内容,会报错。正确的用法如下:

代码语言:javascript
复制
 /**
 * @Value("databases.default.driver")
 */
private $config;

错误的用法:

代码语言:javascript
复制
 /**
 * @Value('databases.default.driver')
 */
private $config;

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

代码语言:javascript
复制
env('APP_NAME', 'Hyperf Skeleton'),

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

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 获取配置
    • 通过依赖注入获取配置
      • 通过注解 @Value() 获取配置
        • 通过 config() 函数获取配置
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档