laravel µÇ¼ Ìøת
×÷ΪһÖÖ¿ªÔ´µÄ php web ¿ò¼Ü£¬laravel Ò»Ö±ÒÔÀ´¶¼±¸ÊÜ web ¿ª·¢ÕßµÄÇàíù¡£ÆäÇ¿Ê¢µÄ¹¦Ð§ÓëÒ×ÓÃÐÔ½ÔΪÆäÓÅÊÆ¡£¹ØÓÚÒ»¸ö web Ó¦ÓÃÀ´Ëµ£¬µÇ¼ÊǷdz£»ù´¡µÄ¹¦Ð§Ö®Ò»¡£ÔÚ laravel ÖУ¬ÎÒÃÇ¿ÉÒԺܿìµØʵÏÖÓû§µÇ¼£¬²¢ÇÒ¾ÙÐÐÌøת¡£
ÏÂÃ棬ÎÒÃǾÍÀ´½²Ò»Ï Laravel ÖÐÓû§µÇ¼ºÍÌøתµÄʵÏÖÒªÁì¡£
Ç°ÖÃÌõ¼þ
ÔÚ×îÏȱàд´úÂë֮ǰ£¬ÎÒÃÇÊ×ÏÈÐèҪȷ±£ÒÔÏÂÌõ¼þ£º
ÓµÓÐ Laravel ÇéÐΣ»
ÒѾ¿ªÆôÁË Laravel µÄÉí·ÝÈÏÖ¤¹¦Ð§¡£
ÈôÊÇÄ㻹ûÓпªÆô Laravel µÄÉí·ÝÈÏÖ¤¹¦Ð§£¬¿ÉÒÔÔÚÖÕ¶ËÀï½øÈëµ½ÏîÄ¿¸ùĿ¼²¢ÊäÈëÒÔÏÂÏÂÁ
php artisan make:auth
¹ØÓÚÒѾ¿ªÆôÉí·ÝÈÏÖ¤¹¦Ð§µÄ Laravel ÏîÄ¿£¬ÎÒÃDZã¿ÉÒÔƾ֤ÒÔÏ°취¾ÙÐÐʵÏÖ¡£
Óû§µÇ¼
ÔÚ Laravel ÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ý±íµ¥À´»ñÈ¡Óû§ÊäÈëµÄÓû§ÃûºÍÃÜÂë¡£Ò»Ñùƽ³£¶øÑÔ£¬ÎÒÃÇ»áÔÚ±íµ¥ÖÐÉèÖÃΪ Post ÒªÁ죬²¢ÇÒÌá½»µ½Ò»¸ö½Ð×ö LoginController µÄ¿ØÖÆÆ÷ÖУ¬ÈçÏ´úÂëËùʾ£º
<form method="POST" action="{{ route('login') }}"> @csrf <div> <label for="email">ÓÊÏä</label> <div> <input id="email" type="email" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus> </div> </div> <div> <label for="password">ÃÜÂë</label> <div> <input id="password" type="password" name="password" required autocomplete="current-password"> </div> </div> <div> <div> <div> <input type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}> <label for="remember"> ¼Ç×ÅÎÒ </label> </div> </div> </div> <div> <div> <button type="submit"> µÇ¼ </button> @if (Route::has('password.request')) <a href="{{ route('password.request') }}"> ÒÅÍüÃÜÂëÁË£¿ </a> @endif </div> </div> </form>
µÇ¼ºó¸´ÖÆ
µ±Óû§ÌîдÍê±íµ¥ÐÅÏ¢ºó£¬µã»÷¡°µÇ¼¡±°´Å¥£¬±ã»áÌá½»Êý¾Ýµ½ LoginController ¿ØÖÆÆ÷ÖС£
ÎÒÃÇÔÚ app/Http/Controllers/Auth/LoginController.php ÎļþÖÐÕÒµ½ login ÒªÁ죬½«¸ÃÒªÁìµÄÄÚÈÝÐÞ¸ÄÈçÏ£º
use IlluminateHttpRequest; public function login(Request $request) { $credentials = $request->only('email', 'password'); if (Auth::attempt($credentials)) { // ÈÏÖ¤ÀֳɺóµÄ²Ù×÷ } return back()->withErrors( ['email' => 'Óû§Ãû»òÃÜÂë¹ýʧ'] ); }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬ÎÒÃÇÊ×ÏÈ»ñÈ¡ÁËÓû§±íµ¥ËùÌá½»µÄÊý¾Ý£¬¼´ÓÊÏäºÍÃÜÂë¡£½ô½Ó×Å£¬ÎÒÃÇʵÑéʹÓà Auth::attempt() ÒªÁì¶ÔÓû§¾ÙÐÐÉí·ÝÈÏÖ¤¡£ÈôÊÇÓû§ÈÏÖ¤Àֳɣ¬ÔòÔÚ if Óï¾äÖоÙÐкóÐø²Ù×÷£¬²»È»·µ»Ø¹ýʧÐÅÏ¢£¬ÌáÐÑÓû§Óû§Ãû»òÃÜÂë¹ýʧ£¬²¢ÖØмÓÔصǼ½çÃæ¡£
Ìøת
µ±Óû§ÀֳɵǼ֮ºó£¬ÎÒÃÇÐèÒª½«Óû§Ìøתµ½Ö¸¶¨µÄÒ³Ãæ¡£ÔÚ Laravel ÖУ¬ÓÐÁ½ÖÖʵÏÖ·½·¨£¬»®·ÖÈçÏ¡£
¿ØÖÆÆ÷·½·¨
ÎÒÃÇ¿ÉÒÔͨ¹ýÔÚ LoginController ¿ØÖÆÆ÷ÖÐÔöÌí authenticated() ÒªÁ죬À´ÊµÏÖÖض¨Ïòµ½Ö¸¶¨Ò³Ãæ¡£¸ÃÒªÌå»áÔÚÓû§ÈÏÖ¤Àֳɺó×Ô¶¯Å²Óá£Ð޸ĺóµÄ LoginController ¿ØÖÆÆ÷´úÂëÈçÏ£º
use IlluminateHttpRequest; public function login(Request $request) { $credentials = $request->only('email', 'password'); if (Auth::attempt($credentials)) { return redirect()->intended('/home'); } return back()->withErrors( ['email' => 'Óû§Ãû»òÃÜÂë¹ýʧ'] ); } protected function authenticated(Request $request, $user) { return redirect('/home'); }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬ÎÒÃÇ¿ÉÒÔ¿´µ½ authenticated() ÒªÁ죬¸ÃÒªÁ콫Óû§Öض¨Ïòµ½ /home Ò³Ãæ¡£
·ÓÉ·½·¨
ÎÒÃÇÒ²¿ÉÒÔÔڵǼÀֳɺó£¬Í¨¹ý×¢²á·Óɵķ½·¨À´ÊµÏÖÖض¨Ïò¡£ÔÚ web.php ÎļþÖУ¬ÎÒÃÇ×¢²áÒÔÏ·ÓÉ£º
Route::get('/home', function () { return view('home'); })->name('home'); Route::group(['middleware' => 'auth'], function () { Route::get('/', function () { return redirect()->route('home'); }); });
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬ÎÒÃÇÔÚ auth ÖÐÑë¼þÖÐ×¢²áÁËĬÈϵĸù·ÓÉ /£¬µ±Óû§µÇ¼Àֳɺ󣬻á×Ô¶¯Öض¨Ïòµ½ /home Ò³ÃæÖС£
×ܽá
ÒÔÉϼ´ÊÇÔÚ Laravel ÖÐÓû§µÇ¼ºÍÌøתµÄÒªÁì¡£Laravel ÌṩµÄÉí·ÝÈÏÖ¤¹¦Ð§£¬ÎªÎÒÃÇ¿ìËÙʵÏÖÓû§µÇ¼ÌṩÁ˼«´óµÄ×ÊÖú¡£Í¬Ê±£¬Í¨¹ý¿ØÖÆÆ÷ºÍ·ÓÉÁ½ÖÖ·½·¨£¬ÎÒÃÇÒ²¿ÉÒÔ¶ÔÓû§µÄÌøת¾ÙÐÐÏ꾡¿ØÖÆ¡£
ÒÔÉϾÍÊÇlaravel µÇ¼ ÌøתµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡