laravel µÇ¼ ĬÈÏÌøת
laravelÊÇÒ»¿îºÜÊÇÊ¢ÐеÄphp¿ò¼Ü£¬ËüÌṩÁËÐí¶à±ã½ÝµÄ¹¦Ð§ºÍÇ¿Ê¢µÄ¹¦Ð§£¬×ÊÖú¿ª·¢Ö°Ô±¸ü¿ì¸üÓÐÓõؿª·¢webÓ¦ÓóÌÐò¡£ÆäÖÐÒ»¸öÇ¿Ê¢µÄ¹¦Ð§¾ÍÊÇÓû§µÇ¼ϵͳ¡£ÔÚlaravelÖУ¬Äã¿ÉÒÔ¿ìËٵؽ¨ÉèÒ»¸öÓû§ÈÏ֤ϵͳ£¬ÈÃÓû§µÇ¼ÄãµÄÓ¦ÓóÌÐò¡£Ö»ÐèʹÓüòÆӵĴúÂ룬¾Í¿ÉÒÔÇáËɵØʵÏÖÐí¶à³£¼ûµÄÓû§ÈÏÖ¤¹¦Ð§£¬ÀýÈç×¢²á£¬µÇ¼£¬ÖØÖÃÃÜÂëµÈ¡£
µ±Óû§ÀֳɵǼÄãµÄÓ¦ÓóÌÐòʱ£¬LaravelĬÈϻὫÓû§Öض¨Ïòµ½Ó¦ÓóÌÐòµÄ¸ùĿ¼¡£È»¶ø£¬ÓÐʱ¼äÄãÏ£ÍûÓû§µÇ¼ºóÄܹ»±»Öض¨Ïòµ½Ìض¨µÄÒ³Ã棬ÀýÈçÓû§µÄСÎÒ˽¼ÒÖ÷Ò³»ò¿ØÖÆÃæ°å¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÏÈÈÝÔõÑùÔÚLaravelÖÐÉèÖÃĬÈϵĵǼÖض¨ÏòURL¡£
Ê×ÏÈ£¬ÈÃÎÒÃÇÀ´¿´¿´LaravelµÄÓû§ÈÏ֤ϵͳÔõÑùÊÂÇé¡£LaravelĬÈÏʹÓÃguardÀ´ÖÎÀíÓû§µÄÈÏÖ¤£¬ÆäÖеġ°web¡± guardÊÇLaravelĬÈÏʹÓõÄguard¡£ÔÚʹÓÃguard֮ǰ£¬ÐèÒªÔÚconfig/auth.phpÎļþÖоÙÐÐÉèÖá£ÔÚ¸ÃÎļþÖУ¬Äã¿ÉÒÔ½ç˵²î±ðµÄguardºÍËüÃǵÄÉèÖá£ÀýÈ磬ÏÂÃæÊÇÒ»¸öconfig/auth.phpÎļþµÄʾÀý£¬ÆäÖнç˵ÁËÁ½¸öguard£º¡°web¡±ºÍ¡°admin¡±¡£
return [ 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => AppModelsUser::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => AppModelsAdmin::class, ], ], ];
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇ¿ÉÒÔ¿´µ½¡°web¡± guardºÍ¡°admin¡± guard¶¼Ê¹ÓÃsession driver¾ÙÐÐÈÏÖ¤£¬ÆäÖС°web¡± guardʹÓá°users¡± provider¾ÙÐÐÓû§ÈÏÖ¤£¬¶ø¡°admin¡± guardʹÓá°admins¡± provider¾ÙÐÐÖÎÀíÔ±ÈÏÖ¤¡£±ðµÄ£¬ÎÒÃÇ»¹¿ÉÒÔÔÚconfig/auth.phpÎļþÖÐÉèÖÃÿ¸öguardµÄĬÈÏÖض¨ÏòURL¡£ÀýÈ磬ÏÂÃæÊÇÒ»¸öʹÓÃĬÈÏÖض¨ÏòURLµÄʾÀý£º
'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', 'redirect' => '/', ], ],
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃǽ«¡°web¡± guardµÄĬÈÏÖض¨ÏòURLÉèÖÃΪ¡°/¡±£¬¼´Ó¦ÓóÌÐòµÄ¸ùĿ¼¡£µ±Óû§ÀֳɵǼºó£¬½«Öض¨Ïòµ½¸ÃURL¡£Í¬Ñù£¬Äã¿ÉÒÔƾ֤ÐèÒª¸ü¸ÄÖض¨ÏòURL£¬ÀýÈ磬½«Æä¸ü¸ÄΪÓû§µÄСÎÒ˽¼ÒÖ÷Ò³»ò¿ØÖÆÃæ°å¡£ÄãÖ»ÐèÒªÔÚguardµÄÉèÖÃÖн«’redirect’Ñ¡Ïî¸ü¸ÄΪÐèÒªÖض¨ÏòµÄURL¼´¿É¡£
¿ÉÊÇ£¬ÈôÊÇÄãµÄÓ¦ÓóÌÐòÖÐÓжà¸öguard£¬ºÃ±ÈÉÏÃæµÄʾÀýÖеġ°web¡±ºÍ¡°admin¡± guard£¬¸ÃÔõÑùÉèÖÃĬÈÏÖض¨ÏòURLÄØ£¿ÔÚÕâÖÖÇéÐÎÏ£¬LaravelÌṩÁËÒ»¸ö¼òÆӵĽâ¾ö¼Æ»®£¬ÈÃÄã¿ÉÒÔÇáËɵØÉèÖÃÿ¸öguardµÄĬÈÏÖض¨ÏòURL¡£
ĬÈÏÇéÐÎÏ£¬LaravelÔÚapp/Http/Controllers/Auth/LoginController.phpÎļþÖÐÌṩÁËÒ»¸öLoginController£¬ÓÃÓÚÖÎÀíÓû§µÇ¼¡£Äã¿ÉÒÔÔڸÿØÖÆÆ÷ÖÐÉèÖÃÿ¸öguardµÄĬÈÏÖض¨ÏòURL¡£ÀýÈ磬ÏÂÃæÊÇÒ»¸ö¸ü¸Ä¡°web¡± guardºÍ¡°admin¡± guardĬÈÏÖض¨ÏòURLµÄʾÀý£º
<?php namespace AppHttpControllersAuth; use AppHttpControllersController; use IlluminateFoundationAuthAuthenticatesUsers; class LoginController extends Controller { use AuthenticatesUsers; protected $redirectTo = '/'; public function __construct() { $this->middleware('guest')->except('logout'); } protected function redirectTo() { if (auth()->user()->isAdmin()) { return '/admin/dashboard'; } else { return '/home'; } } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇÖØдÁËLoginControllerÖеÄredirectTo()ÒªÁ죬²¢Æ¾Ö¤Ä¿½ñÓû§µÄguard·µ»Ø²î±ðµÄÖض¨ÏòURL¡£ÈôÊÇÄ¿½ñÓû§µÄguardÊÇ¡°admin¡±£¬Ôò½«Öض¨Ïòµ½ÖÎÀíÔ±¿ØÖÆÃæ°å£¬²»È»½«Öض¨Ïòµ½Óû§µÄСÎÒ˽¼ÒÖ÷Ò³¡£Äã¿ÉÒÔƾ֤×Ô¼ºµÄÐèÒª¸ü¸ÄÖض¨ÏòURL¡£
ÐèҪעÖصÄÊÇ£¬Ö»ÓÐguardsÖÐÉèÖÃÁË’redirect’Ñ¡Ïî²Å»ªÊ¹ÓÃredirectTo()ÒªÁìÖض¨ÏòÓû§¡£ÈôÊÇÄãûÓÐÔÚconfig/auth.phpÎļþÖÐÉèÖÃ’redirect’Ñ¡ÏÔòÐèҪʹÓÃĬÈϵÄÖض¨ÏòURL¡£ÁíÍ⣬redirectTo()ÒªÁìÖØдʵÖÊÉϲî±ðÓÚÖ±½Ó¸ü¸Ä’redirect’Ñ¡Ïî¡£ÔÚredirectTo()ÒªÁìÖÐÄã¿ÉÒÔ±àдÈκÎÂß¼´úÂëÀ´ÊµÏÖϸÄåµÄ¿ØÖÆÖض¨ÏòURL¡£
×ܵÄÀ´Ëµ£¬ÉèÖÃLaravelĬÈϵǼÖض¨ÏòURLºÜÊǼòÆÓ¡£Äã¿ÉÒÔͨ¹ýconfig/auth.phpÎļþ»òLoginControllerÖеÄredirectTo()ÒªÁìÀ´ÉèÖÃËü¡£ÈôÊÇÄãµÄÓ¦ÓóÌÐòÓжà¸öguard£¬Äã¿ÉÒÔÔÚLoginControllerÖÐϸÄåµØ¿ØÖÆÿ¸öguardµÄĬÈÏÖض¨ÏòURL¡£ÕâЩҪÁì¿ÉÒÔÈÃÄã¸üºÃµØ¿ØÖÆÓû§ÌåÑéºÍÓ¦ÓóÌÐòµÄÁ÷³Ì£¬ÈÃËüÃǸüΪ˳³©ºÍÈËÐÔ»¯¡£
ÒÔÉϾÍÊÇlaravel µÇ¼ ĬÈÏÌøתµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡