添加配置(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}