首先,谈下语言包的问题
1、安装语言包,通过composer进行安装
composer require "overtrue/laravel-lang:dev-master"
2、成功安装后。要到config/app.php中改动一下配置,
'Illuminate\Translation\TranslationServiceProvider'
改为
'Overtrue\LaravelLang\TranslationServiceProvider',
3、再更改 配置
<? php
return [
"min" =>[
"numeric" => "password 至少 :min.",
"string" => "password至少 :min 位.",
],
"confirmed" => "password两次输入不一致",
];
中的
‘locale'=> 'zh-CN'
同一时候在resouces/lang/以下和 en 目录同级别的 的建立一个 zh-CN目录
能够在里面建立一个validation.php 文件
增加一下内容,待一会用
<?php
return [
"min" =>[
"numeric" => "password 至少 :min.",
"string" => "password至少 :min 位.",
],
"confirmed" => "password两次输入不一致",
];
再谈怎样验证信息
在文件的首部增加
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']),
]);
} }