laravelÔõÑùʵÏֵǼºóÌøת¹¦Ð§
laravel ÊÇÏÖÔÚʹÓÃ×îÆÕ±éµÄ php web ¿ò¼Ü£¬ËüµÄµÇ¼¹¦Ð§·Ç¾³£Ó㬶øµÇ¼ÀֳɺóÌøתµ½Ö¸¶¨Ò³ÃæÒ²Êdz£¼ûÐèÇóÖ®Ò»¡£±¾ÎĽ«ÏêϸÏÈÈÝ laravel ÖÐÔõÑùʵÏֵǼºóÌøת¹¦Ð§¡£
1. ×°ÖÃ Laravel
ÈôÊÇÄú»¹Ã»ÓÐ×°Öà Laravel£¬¿ÉÒÔƾ֤ÒÔÏ°취¾ÙÐÐ×°Öãº
×°Öà Laravel ×°ÖÃÆ÷£ºcomposer global require laravel/installer
½¨ÉèÐÂÏîÄ¿£ºlaravel new project_name
½øÈëÏîĿĿ¼£ºcd project_name
Æô¶¯ Laravel ÄÚÖÃЧÀÍÆ÷£ºphp artisan serve
ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«Ê¹ÓÃĬÈÏÉèÖþÙÐпª·¢£¬ÈôÊÇÄúʹÓÃÁËÆäËûÉèÖ㬿ÉÄÜ»áÉÔÓвî±ð¡£
2. ʵÏֵǼ¹¦Ð§
ÔÚ Laravel ÖÐʵÏֵǼ¹¦Ð§ºÜÊǼòÆÓ£¬Ö»ÐèҪʹÓà Auth ÖÐÐļþ¼´¿É¡£ÔÚ routes/web.php ÎļþÖÐÔöÌíÒÔÏ·ÓÉ£º
Route::get('/login', 'LoginController@showLoginForm')->name('login'); Route::post('/login', 'LoginController@login');
µÇ¼ºó¸´ÖÆ
ÕâÁ½¸ö·ÓÉ»®·Ö¶ÔÓ¦µÇ¼ҳÃæµÄÏÔʾºÍµÇ¼²Ù×÷µÄ´¦Öóͷ£¡£ÆäÖУ¬LoginController@showLoginForm ÒªÁìÓÃÓÚÏÔʾµÇ¼±íµ¥£¬LoginController@login ÒªÁìÓÃÓÚ´¦Öóͷ£Óû§Ìá½»µÄµÇ¼ÇëÇó¡£
ÈôÊÇÄú»¹Ã»Óн¨Éè LoginController£¬¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁî¿ìËÙ½¨É裺
php artisan make:controller LoginController
µÇ¼ºó¸´ÖÆ
ÔÚ LoginController ÖУ¬¿ÉÒÔʹÓà Laravel ÌṩµÄ AuthenticatesUsers trait ¿ìËÙʵÏֵǼ¹¦Ð§¡£Ö»ÐèÒªÔÚÀàÖÐÒýÈë´Ë trait ¼´¿É£º
use AuthenticatesUsers; public function __construct() { $this->middleware('guest')->except('logout'); }
µÇ¼ºó¸´ÖÆ
ĬÈÏÇéÐÎÏ£¬µÇ¼Àֳɺó»áÌøתµ½ /home Ò³Ã棬¿ÉÒÔÔÚ Auth\LoginController ÖÐÐÞ¸Ä $redirectTo ÊôÐÔÀ´Ö¸¶¨Óû§µÇ¼ÀֳɺóÌøתµÄÒ³Ãæ¡£
protected $redirectTo = '/dashboard';//ÐÞ¸ÄΪÄãÒªÌøתµÄ·¾¶
µÇ¼ºó¸´ÖÆ
3. ʵÏֵǼºóÌøת
ÔÚ Laravel ÖÐʵÏֵǼºóÌøתҲºÜÊǼòÆÓ£¬Ö»ÐèÒªÔڵǼ¿ØÖÆÆ÷ÖÐÐÞ¸Ä $redirectTo ÊôÐÔ¼´¿É¡£¿ÉÊÇ£¬ÈôÊÇÄúÏ£ÍûµÇ¼ÀֳɺóÌøתµ½Óû§Ö®Ç°»á¼ûµÄÒ³Ã棬ÄÇôÐèÒª¾ÙÐÐÒÔϲÙ×÷£º
½¨Éè RedirectIfAuthenticated ÖÐÐļþ
ÔÚ app/Http/Middleware Ŀ¼Ï½¨Éè RedirectIfAuthenticated.php Îļþ£¬²¢Ìí¼ÓÒÔÏ´úÂ룺
<?php namespace App\Http\Middleware; use Closure; use Illuminate\Support\Facades\Auth; use Illuminate\Http\Request; class RedirectIfAuthenticated { public function handle(Request $request, Closure $next, $guard = null) { if (Auth::guard($guard)->check()) { return redirect()->intended('/dashboard');//ÐÞ¸ÄΪµÇ¼ÀֳɺóÒªÌøתµÄ·¾¶ } return $next($request); } }
µÇ¼ºó¸´ÖÆ
´úÂëÖÐµÄ intended ÒªÌå»áʵÑ齫Óû§Öض¨Ïòµ½ËûÃÇ×î³õÇëÇóÒ³ÃæµÄλÖã¬ÈôÊÇûÓÐÏÈÇ°µÄλÖã¬ÔòĬÈÏÖض¨Ïòµ½Ö¸¶¨µÄ·¾¶¡£
ÔÚ app/Http/Kernel.php ÖÐ×¢²áÖÐÐļþ
ÔÚ app/Http/Kernel.php ÎļþÖÐµÄ $routeMiddleware Êý×éÖÐ×¢²áÐÂÔöµÄÖÐÐļþ£º
protected $routeMiddleware = [ // ... ÆäËûÖÐÐļþÊ¡ÂÔ 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, ];
µÇ¼ºó¸´ÖÆ
Ð޸ĵǼ¿ØÖÆÆ÷
ÔڵǼ¿ØÖÆÆ÷ÖУ¬½«µÇ¼ÀֳɺóÖض¨ÏòµÄÄ¿µÄµØµãÐÞ¸ÄΪ intended ÒªÁ죬¼´¿ÉʵÏֵǼºóÌøתµ½Óû§Ö®Ç°»á¼ûµÄÒ³Ã棺
protected $redirectTo = '/dashboard';//ɾ³ýÕâÐÐ
µÇ¼ºó¸´ÖÆ
public function login(Request $request) { $this->validateLogin($request); if ($this->attemptLogin($request)) { // µÇ¼ÀֳɺóÌøתµ½Óû§Ö®Ç°»á¼ûµÄÒ³Ãæ return redirect()->intended('/dashboard'); } $this->incrementLoginAttempts($request); return $this->sendFailedLoginResponse($request); }
µÇ¼ºó¸´ÖÆ
4. ×ܽá
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇѧϰÁËÔÚ Laravel ÖÐʵÏֵǼºóÌøתÐèÒª¾ÙÐÐÄÄЩ²Ù×÷¡£Ê×ÏÈ£¬ÎÒÃÇÐèҪʹÓà Auth ÖÐÐļþʵÏֵǼ¹¦Ð§¡£È»ºó£¬ÎÒÃÇͨ¹ýÐ޸ĵǼ¿ØÖÆÆ÷ÖÐµÄ $redirectTo ÊôÐÔÀ´ÊµÏֵǼ֮ºóµÄÌøת¡£×îºó£¬ÎÒÃÇͨ¹ý½¨ÉèÖÐÐļþºÍÐ޸ĵǼ¿ØÖÆÆ÷ÖеĴúÂëÀ´ÊµÏֵǼºóÌøתµ½Óû§Ö®Ç°»á¼ûµÄÒ³Ãæ¡£
Ï£Íû±¾ÎÄÄܹ»×ÊÖúÄúÇáËÉʵÏÖ Laravel ÖеĵǼºóÌøת¹¦Ð§£¡
ÒÔÉϾÍÊÇlaravelÔõÑùʵÏֵǼºóÌøת¹¦Ð§µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡