lumen字段规则中文化、多样化

实现中文化

1. 在"resources/lang"文件夹下创建"zn"文件夹

2. 在"zn"文件夹下创建"validation.php",内容如下

<?php
return [

    'foo' => ':attribute不是有效的',

    'accepted' => ':attribute必须接受',
    'active_url' => ':attribute不是一个有效的网址',
    'after' => ':attribute必须是一个在:date之后的日期',
    'alpha' => ':attribute只能由字母组成',
    'alpha_dash' => ':attribute只能由字母、数字和斜杠组成',
    'alpha_num' => ':attribute只能由字母和数字组成',
    'array' => ':attribute必须是一个数组',
    'before' => ':attribute必须是一个在:date之前的日期',
    'between' => [
        'numeric' => ':attribute必须介于:min-:max之间',
        'file' => ':attribute必须介于:min-:max kb之间',
        'string' => ':attribute必须介于:min-:max个字符之间',
        'array' => ':attribute必须只有:min-:max个单元',
    ],
    'boolean' => ':attribute必须为布尔值',
    'confirmed' => ':attribute两次输入不一致',
    'date' => ':attribute不是一个有效的日期',
    'date_format' => ':attribute的格式必须为:format。',
    'different' => ':attribute和:other必须不同。',
    'digits' => ':attribute 必须是:digits位的数字。',
    'digits_between' => ':attribute必须是介于:min和:max位的数字',
    'distinct' => ':attribute已經存在。',
    'email' => ':attribute不是一个合法的邮箱。',
    'exists' => ':attribute不存在',
    'filled' => ':attribute不能为空',
    'image' => ':attribute必须是图片',
    'in' => '已选的属性:attribute非法',
    'in_array' => ':attribute没有在:other中',
    'integer' => ':attribute必须是整数',
    'ip' => ':attribute必须是有效的IP地址',
    'json' => ':attribute必须是正确的JSON格式',
    'max' => [
        'numeric' => ':attribute不能大于:max',
        'file' => ':attribute不能大于:max kb',
        'string' => ':attribute不能大于:max个字符',
        'array' => ':attribute最多只有:max个单元',
    ],
    'mimes' => ':attribute必须是一个:values类型的文件',
    'min' => [
        'numeric' => ':attribute必须大于等于:min',
        'file' => ':attribute大小不能小于:min kb',
        'string' => ':attribute至少为:min个字符',
        'array' => ':attribute至少有:min个单元',
    ],
    'not_in' => '已选的属性:attribute非法',
    'numeric' => ':attribute必须是一个数字',
    'present' => ':attribute必须存在',
    'regex' => ':attribute格式不正确',
    'required' => ':attribute不能为空',
    'required_if' => '当:other为:value时:attribute不能为空',
    'required_unless' => '当:other不为:value时:attribute不能为空',
    'required_with' => '当:values存在时:attribute不能为空',
    'required_with_all' => '当:values存在时:attribute不能为空',
    'required_without' => '当:values不存在时:attribute不能为空',
    'required_without_all' => '当:values都不存在时:attribute不能为空',
    'same' => ':attribute和:other必须相同。',
    'size' => [
        'numeric' => ':attribute大小必须为:size',
        'file' => ':attribute大小必须为:size kb',
        'string' => ':attribute必须是:size个字符',
        'array' => ':attribute必须为:size个单元',
    ],
    'string' => ':attribute必须是一个字符串',
    'timezone' => ':attribute必须是一个合法的时区值',
    'unique' => ':attribute重复',
    'url' => ':attribute格式不正确',

    'attributes' => [
        'name' => '名称',
    ],
];

实现多样化

应用场景:例如前端传的值都为name,而我们需要区分是课程名称还是章节名称,我们需要修改"validation.php"中将attributes数组。

1. 创建"template.json",用于存放模板

{
  "name" : "名称"
}

2. 创建"attributes.json",不需要手动写入任何数据,用于存放当前我们需要的attributes值

例如:当我们需要显示课程名称时,填入{“name”: “课程名称”}
当我们需要显示章节名称时,填入{“name”: “章节名称”}

3.在"validation.php"文件中,将"attributes.json"里的数据赋值给’attributes’

//添加以下代码,获取"attributes.json"里的数据
$filePath = __DIR__ . "/attributes.json";			//获取attributes.json的文件路径
$attributes= json_decode(file_get_contents($filePath),true);			//读取attributes.json文件,并将json格式的数据转化为数组格式
//将$attributes赋值给’attributes‘
return [
  //此处省略上面的代码
  'attributes' => $attributes
];

4.通过拼接的方式来实现多样化

仍然是上面的例子,在项目中有CourseControllerSectionController,这两个控制器分别对课程和章节进行操作
在控制器中重写 “__construct” 方法

CourseController:

public function __construct()
    {
        $filePath = __DIR__ . "/../../../../resources/lang/zn/template.json";
        $template = json_decode(file_get_contents($filePath),true);
        foreach ($template as $key => &$value){
            $template[$key] = '课程' . $value;
        }
        $result = json_encode($$template);
        $attributes = fopen(__DIR__ . "/../../../../resources/lang/zn/attributes.json", "w");
        fwrite($attributes,$result);
    }

SectionController :

public function __construct()
    {
        $filePath = __DIR__ . "/../../../../resources/lang/zn/template.json";
        $template = json_decode(file_get_contents($filePath),true);
        foreach ($template as $key => &$value){
            $template[$key] = '章节' . $value;
        }
        $result = json_encode($$template);
        $attributes = fopen(__DIR__ . "/../../../../resources/lang/zn/attributes.json", "w");
        fwrite($attributes,$result);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值