PHP code example of carlin / laravel-dict

1. Go to this page and download the library: Download carlin/laravel-dict library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

carlin / laravel-dict example snippets



return [
	'store'=>env('DICT_ENUM_STORE', env('CACHE_DRIVER', 'file')), //缓存驱动
	'cache-key'=>env('DICT_ENUM_CACHE_KEY', 'dict-cache-key'), //缓存key
	'cache-ttl'=> (int)env('DICT_ENUM_CACHE_TTL', 60 * 60 * 24 * 30), //缓存时间, 默认30天
	'enum-scan-paths'=>[
		//base_path('app/Enums/*.php'), //扫描路径
	],
];


namespace App\Enums;

use Carlin\LaravelDict\Dict;
use BenSampo\Enum\Enum;

class BaseEnum extends Enum {
    public static function getDescription(mixed $value): string
    {
        return Dict::getDescription(static::class, $value) ?? parent::getDescription($value);
    }

    public static function descriptions(): array
    {
        return Dict::getEnums(static::class);
    }
}




namespace App\Enums;

use Carlin\LaravelDict\Attributes\EnumClass;
use Carlin\LaravelDict\Attributes\EnumProperty;

#[EnumClass(
__CLASS__,  //枚举类名
'布尔整型枚举', //枚举描述
'webApi', //枚举分组
[
    'test'=>1 //业务拓展字段(选填)
]
)] //枚举类注解
class BoolIntEnums extends BaseEnum
{
    //#[EnumProperty('是', ['test'=>2])] //业务拓展字段(选填)
    #[EnumProperty('是', ['test'=>2])]
    public const TRUE = 1;

    //#[EnumProperty('否', ['test'=>1])]
    #[EnumProperty('否')]
    public const FALSE = 0;
}



use Carlin\LaravelDict\Dict;
//清除缓存
Dict::clearDictCache();
//获取枚举信息
Dict::getEnums(BoolIntEnums::class); //获取枚举信息
//getDescription
Dict::getDescription(BoolIntEnums::class, 1); //获取枚举描述
//获取枚举字典列表
Dict::getDict();
//获取指定group字典
Dict::getByGroup('webApi');