註冊php
/app/Http/Controller/Auth/RegisterController.phpajax
<?php namespace App\Http\Controllers\Auth; use App\Models\User; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Validator; use Illuminate\Foundation\Auth\RegistersUsers; class RegisterController extends Controller { /* |-------------------------------------------------------------------------- | Register Controller |-------------------------------------------------------------------------- | | This controller handles the registration of new users as well as their | validation and creation. By default this controller uses a trait to | provide this functionality without requiring any additional code. | */ use RegistersUsers; /** * Where to redirect users after registration. * * @var string */ protected $redirectTo = '/'; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest'); } /** * Get a validator for an incoming registration request. * * @param array $data * @return \Illuminate\Contracts\Validation\Validator */ protected function validator(array $data) { return Validator::make($data, [ //'name' => ['required', 'string', 'max:255'], 'email' => ['required', 'string', 'email', 'max:100', 'unique:users'], // 'password' => ['required', 'string', 'min:8', 'confirmed'], 'password' => ['required', 'string', 'min:8'], ]); } /** * Create a new user instance after a valid registration. * * @param array $data * @return \App\User */ protected function create(array $data) { return User::create([ 'email' => $data['email'], 'password' => Hash::make($data['password']), ]); } protected function registered(Request $request) { return $request->ajax() ? response()->json('success') : redirect($this->redirectPath()); } }
其中json
use App\Models\User; 須要修改成本身的模型app
use Illuminate\Http\Request; 添加ide
protected $redirectTo = '/'; 修改成註冊成功後跳轉的URLui
return Validator::make 根據本身的字段進行修改this
在最後添加spa
protected function registered(Request $request) { return $request->ajax() ? response()->json('success') : redirect($this->redirectPath()); }
登陸code
/app/Http/Controller/Auth/LoginController.phpblog
<?php namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\AuthenticatesUsers; use Illuminate\Http\Request; class LoginController extends Controller { /* |-------------------------------------------------------------------------- | Login Controller |-------------------------------------------------------------------------- | | This controller handles authenticating users for the application and | redirecting them to your home screen. The controller uses a trait | to conveniently provide its functionality to your applications. | */ use AuthenticatesUsers; /** * Where to redirect users after login. * * @var string */ protected $redirectTo = '/'; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest')->except('logout'); } protected function authenticated(Request $request) { return $request->ajax() ? response()->json('success') : redirect($this->redirectPath()); } }
其中
use Illuminate\Http\Request; 添加
在底部添加
protected function authenticated(Request $request) { return $request->ajax() ? response()->json('success') : redirect($this->redirectPath()); }