laravel login ÐÞ¸Ä
laravelÊÇÒ»¿îºÜÊÇÊ¢ÐеĿªÔ´php¿ò¼Ü£¬±»ÆÕ±éÓ¦ÓÃÓÚwebÓ¦ÓóÌÐò¿ª·¢¡£ÆäÖУ¬ÊµÏÖÓû§µÇ¼¹¦Ð§ÊÇwebÓ¦ÓóÌÐòµÄ»ù±¾ÐèÇó¡£ÔÚÕâƪÎÄÕÂÖУ¬ÎÒÃǽ«ÌÖÂÛÔõÑùÔÚlaravelÓ¦ÓóÌÐòÖÐÐ޸ĵǼ¹¦Ð§£¬ÒÔÖª×ãÌض¨ÐèÇó»òÌá¸ßÓû§ÌåÑé¡£
Ð޸ķÓÉ
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÐÞ¸Älogin·ÓÉ¡£Ä¬ÈÏÇéÐÎÏ£¬LaravelÓ¦ÓóÌÐòµÄµÇ¼·ÓÉΪ/auth/login¡£¿ÉÊÇ£¬ÎÒÃÇ¿ÉÄÜÏ£Íû½«Æä¸ü¸ÄΪij¸ö×Ô½ç˵·¾¶¡£ÔÚLaravelÖУ¬¿ÉÒÔͨ¹ýÐÞ¸Äroutes/web.phpÎļþÀ´ÊµÏÖÕâһĿµÄ¡£
ÏÂÃæÊÇÒ»¸öʾÀý£¬½«µÇ¼·ÓÉ´Ó/auth/loginÐÞ¸ÄΪ/login£º
Route::get('/login', 'AuthLoginController@showLoginForm')->name('login'); Route::post('/login', 'AuthLoginController@login');
µÇ¼ºó¸´ÖÆ
Ð޸ĵǼ±íµ¥ÊÓͼ
½ÓÏÂÀ´£¬ÎÒÃÇ¿ÉÒÔÐ޸ĵǼ±íµ¥ÊÓͼÒÔÖª×ãÌض¨ÐèÇó»òÌá¸ßÓû§ÌåÑé¡£ÔÚLaravelÖУ¬µÇ¼±íµ¥ÊÓͼÓÉresources/views/auth/login.blade.phpÎļþ½ç˵¡£
ÏÂÃæÊÇÒ»¸öʾÀý£¬ÎÒÃÇÔÚ±íµ¥ÖÐÌí¼ÓÁËÒ»¸ö¡°¼Ç×ÅÎÒ¡±¸´Ñ¡¿ò£¬ÒÔÔÊÐíÓû§Ñ¡ÔñÊÇ·ñ¼á³ÖµÇ¼״̬£º
<form method="POST" action="{{ route('login') }}"> @csrf <div class="form-group"> <label for="email">{{ __('E-Mail Address') }}</label> <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus> @error('email') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> <div class="form-group"> <label for="password">{{ __('Password') }}</label> <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="current-password"> @error('password') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> <div class="form-group form-check"> <input type="checkbox" class="form-check-input" id="remember" name="remember"> <label class="form-check-label" for="remember">{{ __('Remember Me') }}</label> </div> <button type="submit" class="btn btn-primary">{{ __('Login') }}</button> </form>
µÇ¼ºó¸´ÖÆ
Ð޸ĵǼ¿ØÖÆÆ÷
×îºó£¬ÎÒÃÇÐèÒªÐ޸ĵǼ¿ØÖÆÆ÷£¬ÒÔÏìÓ¦±íµ¥Ìá½»²¢Ö´ÐÐÌض¨²Ù×÷¡£ÔÚLaravelÖУ¬Ä¬ÈϵĵǼ¿ØÖÆÆ÷Ϊapp/Http/Controllers/Auth/LoginController.php¡£
ÏÂÃæÊÇÒ»¸öʾÀý£¬ÎÒÃÇÌí¼ÓÁËÒ»¸öredirectTo()ÒªÁ죬ÒÔÔÚÓû§ÀֳɵǼºó×Ô¶¯Öض¨ÏòÖÁÌض¨Ò³Ã棺
<?php namespace AppHttpControllersAuth; use AppHttpControllersController; use IlluminateFoundationAuthAuthenticatesUsers; class LoginController extends Controller { use AuthenticatesUsers; /** * Where to redirect users after login. * * @var string */ protected $redirectTo = '/dashboard'; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest')->except('logout'); } /** * Get the post login redirect path. * * @return string */ public function redirectTo() { if (auth()->user()->isAdmin()) { return '/admin/dashboard'; } return '/dashboard'; } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöʾÀýÖУ¬redirectTo()ÒªÁìʹÓÃauth()¸¨Öúº¯ÊýÀ´¼ì²éÓû§½ÇÉ«£¬È»ºóÖض¨Ïòµ½ÏìÓ¦µÄÒ³Ãæ¡£ÕâΪLaravelµÇ¼¹¦Ð§ÌṩÁ˸ü´óµÄÎÞаÐԺͿÉÀ©Õ¹ÐÔ¡£
×ܽá
ͨ¹ý±¾ÎĵÄÏÈÈÝ£¬¶ÁÕßÒѾÏàʶÁËÔõÑùÔÚLaravelÓ¦ÓóÌÐòÖÐÐ޸ĵǼ¹¦Ð§¡£Õâ¸öÀú³Ì°üÀ¨Ð޸ĵǼ·ÓÉ¡¢Ð޸ĵǼ±íµ¥ÊÓͼºÍÐ޸ĵǼ¿ØÖÆÆ÷¡£ÕâЩˢпÉÒÔÌá¸ßÓû§ÌåÑ飬Ҳ¿Éƾ֤Ìض¨ÐèÇóÀ´ÊµÏÖ¶¨ÖÆ»¯µÄµÇ¼¹¦Ð§¡£Ëæ×ÅLaravelÓ¦ÓóÌÐòµÄÉú³¤ºÍÓû§ÐèÇóµÄת±ä£¬¸ü¶àµÄµÇ¼¹¦Ð§ÐÞ¸ÄÒ²½«°üÀ¨ÓÚ´Ë¡£
ÒÔÉϾÍÊÇlaravel login Ð޸ĵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡