首先,谈下语言包的问题
1、安装语言包,通过composer进行安装
composer require "overtrue/laravel-lang:dev-master"
2、安装成功后,要到config/app.php中修改一下配置,
‘Illuminate\Translation\TranslationServiceProvider‘
改为
‘Overtrue\LaravelLang\TranslationServiceProvider‘,
3、再更改 配置
<?php return [ "min" =>[ "numeric" => "密码 至少 :min.", "string" => "密码至少 :min 位.", ], "confirmed" => "密码两次输入不一致", ];
中的
‘locale‘=> ‘zh-CN‘
同时在resouces/lang/下面和 en 文件夹同级别的 的建立一个 zh-CN文件夹
可以在里面建立一个validation.php 文件
加入一下内容,待一会用
<?php return [ "min" =>[ "numeric" => "密码 至少 :min.", "string" => "密码至少 :min 位.", ], "confirmed" => "密码两次输入不一致", ];
再谈如何验证信息
在文件的首部加入
use App\Services\Registrar; 例如我要创建一个 function createUser() { $data = array( 'name' => 'admins', 'email' => '[email protected]', 'password' => '12345' ); $register = new Registrar; $validator = $register->validator($data); //通过Registrar类中的验证方法验证输入信息 if(!$validator->fails()) { $result = $register->create($data); if($result) { return array('code' => 1, 'info' => '用户创建成功'); } } else { $messages = $validator->messages(); return array('code' => 0, 'info'=> $messages->first()); } } Registrar类的写法如下,几乎为默认没有什么改动 <?php namespace App\Services; use App\User; use Validator; use Illuminate\Contracts\Auth\Registrar as RegistrarContract; class Registrar implements RegistrarContract { /** * Get a validator for an incoming registration request. * * @param array $data * @return \Illuminate\Contracts\Validation\Validator */ public function validator(array $data) { $validator = Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:users', 'password' => 'required|min:6', ]); return $validator; } /** * Create a new user instance after a valid registration. * * @param array $data * @return User */ public function create(array $data) { return User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); } }
时间: 2024-10-12 17:29:44