首先,谈下语言包的问题
composer require "overtrue/laravel-lang:dev-master"
‘Illuminate\Translation\TranslationServiceProvider‘
<?php return [ "min" =>[ "numeric" => "密码 至少 :min.", "string" => "密码至少 :min 位.", ], "confirmed" => "密码两次输入不一致", ];
<?php return [ "min" =>[ "numeric" => "密码 至少 :min.", "string" => "密码至少 :min 位.", ], "confirmed" => "密码两次输入不一致", ];
use App\Services\Registrar; 例如我要创建一个 function createUser() { $data = array( 'name' => 'admins', 'email' => 'admin@admin.comd', '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']), ]); } }
原文地址:http://blog.csdn.net/rchangchun/article/details/46352653