Java教程

hyperf 配置

本文主要是介绍hyperf 配置,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

添加配置(author) config/config.php

<?php

declare(strict_types=1);

use Hyperf\Contract\StdoutLoggerInterface;
use Psr\Log\LogLevel;

return [
    'app_name' => env('APP_NAME', 'skeleton'),
    'app_env' => env('APP_ENV', 'dev'),
    'scan_cacheable' => env('SCAN_CACHEABLE', false),
    StdoutLoggerInterface::class => [
        'log_level' => [
            LogLevel::ALERT,
            LogLevel::CRITICAL,
            LogLevel::DEBUG,
            LogLevel::EMERGENCY,
            LogLevel::ERROR,
            LogLevel::INFO,
            LogLevel::NOTICE,
            LogLevel::WARNING,
        ],
    ],
    //自定义配置
    'author'=>'huyongjian',
];

测试代码

Controllor/IndexController.php

<?php

declare(strict_types=1);
namespace App\Controller;

//第一步导入命名空间
use Hyperf\Contract\ConfigInterface;
use Hyperf\Di\Annotation\Inject;


use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\RequestMapping;


/**
 * @AutoController()
 */
class IndexController extends AbstractController
{

        //第二步 添加注解
        /**
         * @Inject()
         * @var ConfigInterface
         */
        private $config;

        /**
         * @RequestMapping(methods="get,post")
         */
        public function index()
        {

                //第三步 获取配置值
                //获取 config.php 里的内容
                $appName = $this->config->get('app_name','');
                $author = $this->config->get('author','');
                return [
                        'app_name' => $appName,
                        'author' => $author
                ];
        }
}

测试结果

{"app_name":"skeleton","author":"huyongjian"}

获取autoload/server.php配置

index/index修改

 //获取autoload/server.php里的内容
 $serverMode = $this->config->get('server.mode','');
 $serverSockType = $this->config->get('server.servers.0.sock_type','');

  return [
    'server_mode' => $serverMode,
    'server_sock_type' => $serverSockType,
  ];
  */

测试结果

{"server_mode":2,"server_sock_type":1}
这篇关于hyperf 配置的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!