六 hyperf 配置,常量

274 阅读1分钟

一 常量(常量和对应的msg)

//php bin/hyperf.php gen:constant ErrorCode 创建

declare(strict_types=1);

namespace App\Constants;

use Hyperf\Constants\AbstractConstants;
use Hyperf\Constants\Annotation\Constants;

#[Constants]
class ErrorCode extends AbstractConstants
{
    /**
     * @Message("Server Error!")
     */
    const SERVER_ERROR = 500;

    /**
     * @Message("系统参数错误")
     */
    const SYSTEM_INVALID = 700;
}

//用法
echo ErrorCode::SYSTEM_INVALID;
echo ErrorCode::getMessage(ErrorCode::SERVER_ERROR);

二 取配置数据

//配置于config.php
$config = [
    'app_name' =>  config("app_name"),
    'demo_a' => config('config_demo.a'),  //二级
    'demo_c' => config('config_demo.b.c'), //3级
    ];
    
    
#使用注解
use Hyperf\Config\Annotation\Value;


#[Value("app_env")]
private $appEnv;

echo $this->appEnv;    

image.png

三 取不同配置文件的配置数据

//对应config/autoload/redis.php
'redis' => config('redis')['default']['host']

四 小套路,不存在且取默认值

$env = env("a", 1);
$config = config("b",2);