ÔõÑùʹÓÃLaravelʵÏֵǼ²¢¾ÙÐÐÌøת
laravelÊÇÒ»¿î¹ãÊܽӴýµÄphp¿ª·¢¿ò¼Ü£¬Ëü¾ßÓÐÒ×ÓÚѧϰ¡¢¸ßЧ¿ª·¢¡¢Çå¾²Îȹ̵ÈÌص㣬±»Öڶ࿪·¢ÕßËùϲ»¶¡£ÔÚlaravelÖУ¬µÇ¼ºÍÌøתÊǷǾ³£¼ûµÄ²Ù×÷£¬ÎÒÃÇ¿ÉÒÔºÜÇáËɵØʵÏÖÕâЩ¹¦Ð§¡£
µÇ¼¹¦Ð§ÊÇÍøÕ¾±Ø²»¿ÉÉٵĹ¦Ð§Ö®Ò»£¬Ëü¿ÉÒÔΪÓû§Ìṩ˽È˿ռ䣬ÒÔ¼°¸ü¸ß¼¶±ðµÄ»á¼ûȨÏÞ¡£ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔÇáËɵØʵÏֵǼ¹¦Ð§¡£ÔÚÕâÀÎÒÃǽ«Ì½ÌÖÔõÑùʹÓÃLaravelʵÏֵǼ²¢¾ÙÐÐÌøת¡£
½¨ÉèÓû§ÈÏ֤ϵͳ
Ê×ÏÈ£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öÓû§ÈÏ֤ϵͳ¡£LaravelÒѾΪÎÒÃÇÌṩÁËÒ»¸öÏֳɵÄÈÏ֤ϵͳ£¬ÎÒÃÇÖ»ÐèÒªÔËÐÐÒÔÏÂÏÂÁî¼´¿É£º
php artisan make:auth
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁ×Ô¶¯ÌìÉúµÇ¼¡¢×¢²á¡¢ÒÅÍüÃÜÂëµÈ¹¦Ð§£¬Í¬Ê±ÌìÉúÏìÓ¦µÄÊÓͼºÍ¿ØÖÆÆ÷¹©ÎÒÃÇʹÓá£
½¨ÉèloginÊÓͼ
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öÓÃÓڵǼµÄÊÓͼ¡£ÎÒÃÇ¿ÉÒÔʹÓÃLaravelÌṩµÄÄ£°åÒýÇæBladeÀ´ÊµÏÖÊÓͼµÄ½¨Éè¡£ÔÚresources/views/authÎļþ¼ÐϽ¨ÉèÒ»¸ölogin.blade.phpµÄÎļþ£¬´úÂëÈçÏ£º
@extends('layouts.app') @section('content') <div> <div> <div> <div> <div>{{ __('Login') }}</div> <div> <form> @csrf <div> <label>{{ __('E-Mail Address') }}</label> <div> <input> @error('email') <span> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div> <label>{{ __('Password') }}</label> <div> <input> @error('password') <span> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div> <div> <div> <input> <label> {{ __('Remember Me') }} </label> </div> </div> </div> <div> <div> <button> {{ __('Login') }} </button> @if (Route::has('password.request')) <a> {{ __('Forgot Your Password?') }} </a> @endif </div> </div> </form> </div> </div> </div> </div> </div> @endsection
µÇ¼ºó¸´ÖÆ
ÔÚ¸ÃÊÓͼÖУ¬ÎÒÃÇʹÓÃÁËBootstrapµÄÑùʽ£¬²¢ÇÒʹÓÃÁËLaravelÌṩµÄ±íµ¥ÌìÉúº¯ÊýÀ´ÌìÉú±íµ¥¡£³ý´ËÖ®Í⣬ÎÒÃÇ»¹ÔÚ±íµ¥ÖÐÌí¼ÓÁË@csrfÖ¸ÁÓÃÓÚ±ÜÃâ¿çÕ¾µãÇëÇóαÔ죨CSRF£©¹¥»÷¡£
´¦ÀíµÇ¼ÇëÇó
µÇ¼±íµ¥½¨ÉèºÃÖ®ºó£¬ÎÒÃÇ»¹ÐèÒª¶ÔµÇ¼ÇëÇó¾ÙÐд¦Àí¡£ÎÒÃÇ¿ÉÒÔÔÚAuth\LoginControllerÀàÖУ¬Ê¹ÓÃLaravelÌṩµÄauthenticate£¨£©ÒªÁìÀ´ÑéÖ¤Óû§µÄÈÏÖ¤ÐÅÏ¢¡£ÈôÊÇÑéÖ¤Àֳɣ¬ÎÒÃÇ¿ÉÒÔͨ¹ýLaravelµÄredirect£¨£©ÒªÁìÀ´¾ÙÐÐÌøת¡£
´úÂëÈçÏ£º
public function authenticate(Request $request) { $credentials = $request->only('email', 'password'); if (Auth::attempt($credentials)) { // ÈÏ֤ͨ¹ý£¬Ìøתµ½Ö¸¶¨Ò³Ãæ return redirect()->intended('dashboard'); } else { // ÈÏ֤ʧ°Ü£¬Ìøתµ½µÇ¼ҳÃæ return redirect()->route('login')->with('error', 'Invalid credentials'); } }
µÇ¼ºó¸´ÖÆ
ÔڸôúÂëÖУ¬ÎÒÃÇʹÓÃÁËAuth::attempt()ÒªÁìÀ´¶ÔÓû§µÄÊäÈë¾ÙÐÐÑéÖ¤¡£
ÈôÊÇÑé֤ͨ¹ý£¬ÎÒÃÇ¿ÉÒÔʹÓÃLaravelµÄintended£¨£©ÒªÁìÀ´¾ÙÐÐÌøת¡£¸ÃÒªÁ콫×Ô¶¯Ìøתµ½Óû§ÏëÒª»á¼ûµÄÒ³Ã棬ÈôÊÇÓû§Ã»ÓÐÖ¸¶¨£¬ÔòĬÈÏÌøתµ½¡°dashboard¡±Ò³Ãæ¡£
ÈôÊÇÈÏ֤ʧ°Ü£¬ÔòʹÓÃLaravelµÄredirect£¨£©ÒªÁ콫Óû§ÖØÐÂÌøתµ½µÇ¼ҳÃ棬²¢ÔÚÌøתʱת´ï¹ýʧÐÂÎÅ¡£
Ìøתµ½Ö¸¶¨Ò³Ãæ
ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃlaravelÖеÄRedirectÀàºÍURLÀàÀ´¾ÙÐÐÌøת¡£ÈôÊÇÎÒÃÇÒªÖض¨Ïòµ½Â·ÓÉ£¬Ôò¿ÉÒÔʹÓÃLaravelµÄredirect£¨£©ÒªÁì¡£ÈôÊÇÎÒÃÇÒªÖض¨Ïòµ½URL£¬Ôò¿ÉÒÔʹÓÃLaravelµÄURL::to£¨£©ÒªÁì¡£
´úÂëʾÀýÈçÏ£º
// Öض¨Ïòµ½Â·ÓÉ return redirect()->route('dashboard'); // Öض¨Ïòµ½URL return redirect(URL::to('/dashboard'));
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀú³ÌÖУ¬Çë¼Ç×Å£¬Äã¿ÉÒÔÔÚ¿ØÖÆÆ÷»ò·ÓÉÎļþÖÐ×ÔÓÉʹÓÃÉÏÊöÌøתҪÁ죬ÒÔ±ãÌøתµ½×¼È·µÄÒ³Ãæ¡£
½áÂÛ
Laravel ÊÇÒ»¸ö¹¦Ð§Ç¿Ê¢ÇÒÒ×ÓÚʹÓÃµÄ PHP ¿ò¼Ü£¬ËüÌṩÁËÐí¶à¹¤¾ßºÍ¹¦Ð§À´¼ò»¯ Web ¿ª·¢¡£ÔÚÕâƪÎÄÕÂÖУ¬ÎÒÃǽÌÊÚÁËÔõÑùÔÚLaravelÖÐʵÏÖÓû§µÇ¼ºÍÌøתµ½Ö¸¶¨Ò³ÃæµÄÀú³Ì¡£
ÎÒÃÇÊ×ÏȽ¨ÉèÁËÒ»¸öÓû§ÈÏ֤ϵͳ£¬È»ºó½¨ÉèÁ˵Ǽ±íµ¥¡¢´¦ÀíµÇ¼ÇëÇó¡¢ÒÔ¼°Ê¹ÓÃRedirectÀàºÍURLÀà¾ÙÐÐÌøתµÄ´úÂë¡£ÕâЩÊÖÒÕºÍÒªÁì¿ÉÒÔ×ÊÖú¿ª·¢Ö°Ô±¿ìËÙʵÏÖÇ¿Ê¢µÄµÇ¼ºÍÌøת¹¦Ð§¡£
ͬʱֵµÃ×¢ÖصÄÊÇ£¬Çå¾²ÎȹÌÐÔÓÀÔ¶ÊÇÒ»¸öºÜÊÇÖ÷ÒªµÄÎÊÌ⣬ÎÒÃÇÐèҪȷ±£´úÂë¿â¾ÓÉÑÏ¿á²âÊÔ²¢Óë×îеÄÇå¾²±ê×¼Ïà·ûÒÔ¼á³ÖϵͳµÄÎȹÌÐÔºÍÇå¾²ÐÔ¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃLaravelʵÏֵǼ²¢¾ÙÐÐÌøתµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡