php<?php Route::controllers([ 'auth' => 'Auth\AuthController', ]);
這種路由配置方式對應的路徑方式是 訪問的方式+方法名 如在瀏覽器中使用get方式去訪問register方法 那麼這個路由會自動配置到這個類下面的getRegister方法。post也是同樣php
我在AuthControlle裏創建一個本身的視圖laravel
php<?php public function getRegister(){ return view("auth.register"); }
上面已經說了路由的解析方式 只要配好路由就能夠了git
接下來我會把全部的代碼貼出來再代碼中去講解怎麼重寫 一樣這個文件位於AuthController下正則表達式
php
<?php public function postRegister(UserRegisterRequest $req){ //驗證經過 註冊用戶 $data = $req->all(); $data['register_ip'] = $req->ip(); $user = $this->registrar->create($data); return redirect()->intended('/'); } ?>
你們會發現整個註冊功能很是簡單 那麼具體能夠在哪裏作了註冊的限制呢
實際上是在UserRegisterRequest這個文件裏去對全部填寫的表單數據進行了控制數據庫
php<?php namespace App\Http\Requests; use App\Http\Requests\Request; use Config; class UserRegisterRequest extends Request { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { //自定義的電話號碼正則表達式 $regex = Config::get('constant.phone_number_regex'); return [ //對註冊表單提交的信息進行驗證 "username" => ['required','min:3','max:16','unique:users'], "phone_number" => ['required','min:3','max:16','unique:users'], "password" => ['required','min:6','max:16','confirmed'], "verify_code" => ['required','digits:4'], ]; } public function sanitize() { return $this->all(); } }
咱們能夠經過laravel自帶的php artisan make:request 建立一個請求類
這個類能夠對全部請求的數據進行控制,在rule裏面定義好規則就能夠控制請求的的數據,若是符合規則那麼會繼續訪問。segmentfault
註冊的目的就是爲了將符合條件的數據寫入用戶表中 既然改寫了請求的數據 那麼天然也要改寫請求的操做瀏覽器
註冊成功後添加到用戶數據庫中 是在上面的 postRegister中的這一段代碼來實現的app
php<?php $user = $this->registrar->create($data);
找到這段代碼 這段代碼位於 services下的Registrar.php下函數
php<?php public function create(array $data) { $properties = [ 'username' => $data['username'], 'password' => bcrypt($data['password']), 'register_time' => Carbon::now()->toDateTimeString(), 'register_ip' => $data['register_ip'] ]; return User::create($properties); $user = new User(); $user->save(); }
將這個處理函數改爲符合本身業務邏輯的函數就好了post