當您安裝laravel,它帶有一個默認的登錄,使用一個特點:
class AuthController extends Controller {
use AuthenticatesAndRegistersUsers;
/**
* Create a new authentication controller instance.
*
* @param \Illuminate\Contracts\Auth\Guard $auth
* @param \Illuminate\Contracts\Auth\Registrar $registrar
* @return void
*/
public function __construct(Guard $auth, Registrar $registrar)
{
$this->auth = $auth;
$this->registrar = $registrar;
$this->middleware('guest', ['except' => 'getLogout']);
}
}
這個類使用存儲在登錄的特徵:vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers.php
您可以覆蓋從這個類的方法把你自己的邏輯,例如在類AuthController
你可以定義一個新:
function postLogin(){
//your new logic for login
}
它會尊重你的功能,而不是特性功能。 反正postLogin
從auth trait
背後的邏輯是:
public function postLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|email', 'password' => 'required',
]);
$credentials = $request->only('email', 'password');
if ($this->auth->attempt($credentials, $request->has('remember')))
{ //this if validate if the user is on the database line 1
return redirect()->intended($this->redirectPath());
//this redirect if user is the db line 2
}
return redirect($this->loginPath())
->withInput($request->only('email', 'remember'))
->withErrors([
'email' => $this->getFailedLoginMessage(),
]);
//redirect again to login view with some errors line 3
}
你可以做兩件事情:
- 編輯性狀本身(糟糕的做法)把自己的邏輯
- 定義您自己的
postLogin
函數AuthController
並複製邏輯,但用您自己的自定義邏輯進行編輯。
編輯 更conrete與你的觀點:
用戶會進入登錄頁面:您可以使用默認的登錄頁面,laravel給你,或者你可以覆蓋getLogin
功能redircet你自己的看法。
用戶提交的登錄頁面:表單動作必須是:{{ url('/auth/login') }}
或任何路線,你投入postLogin()
應用,如果用戶在數據庫將檢查:在代碼行1
3 。1(如果用戶沒有在數據庫|它會發送到第三方的請求,並檢查是否登錄成功):在代碼行3
3.2如果用戶在數據庫驗證密碼:在代碼行2
你認爲哪些事情比默認auth與laravel有更好的解決方案? –
@SafoorSafdar好吧,拉拉維爾的Auth出來了。通常使用這種類型的身份驗證。但是如果你需要任何額外的東西或重新邏輯的東西。你需要這樣做。在我看來,卡洛斯說,我認爲「更好的解決方案」。 – xdevnull