尊龙凯时人生就是博

laravel ÉÏ°¶ºóÌøת

ÔÚ¿ª·¢ web Ó¦ÓóÌÐòʱ £¬Óû§µÇ¼ÈÏÖ¤ÊÇÒ»¸ö±Ø²»¿ÉÉٵĹ¦Ð§¡£laravel ¿ò¼ÜÌṩÁ˶àÖÖ·½·¨ÊµÏÖÓû§ÈÏÖ¤ £¬²¢ÇÒ»¹ÌṩÁËÒ»Ì×ĬÈϵÄÉí·ÝÈÏ֤ϵͳ£¨¼´ laravelilluminateauth£© £¬Àû±ã¿ª·¢ÕßÔÚÓ¦ÓóÌÐòÖÐʵÏÖÓû§×¢²á¡¢µÇ¼¡¢Í˳öµÈ¹¦Ð§¡£

¹ØÓڵǼÈÏÖ¤Àֳɺó £¬ÎÒÃǾ­³£Ï£ÍûÌøתµ½Ìض¨Ò³Ãæ £¬ºÃ±ÈÓû§µÄСÎÒ˽ÈË×ÊÁÏÒ³ £¬»òÕßij¸öÌض¨¹¦Ð§Ò³¡£ÔÚ Laravel ¿ò¼ÜÖÐ £¬ÊµÏֵǼºóÌøתºÜÊǼòÆÓ¡£

±¾ÎĽ«ÏÈÈÝ Laravel ¿ò¼ÜÖÐÓû§µÇ¼ºóÌøתµÄ¼¸ÖÖ·½·¨¡£

Laravel ¿ò¼ÜĬÈϵĵǼÌøת

LaravelIlluminateAuth ÌṩµÄÉí·ÝÈÏ֤ϵͳĬÈÏÉèÖÃÁËÓû§µÇ¼ºóµÄÌøת¡£ÔÚ config/auth.php ÉèÖÃÎļþÖÐ £¬ÓÐÈçÏÂĬÈÏÉèÖãº

'redirect' => [
    'login' => '/login',
    'logout' => '/logout',
    'home' => '/home',
    'register' => '/register',
    'verify' => '/email/verify',
    'reset' => '/password/reset',
    'confirm' => '/password/confirm',
  ],

µÇ¼ºó¸´ÖÆ

ÆäÖÐ £¬’home’ ÌåÏֵǼºóµÄÌøתҳÃæ £¬Ä¬ÒÔΪ /home ·¾¶¡£ÈôÊÇÐèÒªÐÞ¸ÄĬÈÏÌøתҳÃæ £¬Ö»ÐèÒª½«¸Ã·¾¶ÐÞ¸ÄΪ×Ô¼ºÏëÒªµÄÒ³Ãæ¼´¿É¡£

ÊÖ¶¯Ö¸¶¨Ìøת·¾¶

ÈôÊÇÐèÒªÔÚ¿ØÖÆÆ÷ÖÐÊÖ¶¯Ö¸¶¨µÇ¼ºóµÄÌøתҳÃæ £¬ÎÒÃÇ¿ÉÒÔʹÓà Laravel ¿ò¼ÜÌṩµÄ RedirectResponse ʵÀý £¬Í¨¹ý redirect() ÒªÁìʵÏÖ¡£

ÀýÈç £¬ÔÚÓû§¿ØÖÆÆ÷ÖÐ £¬ÎÒÃÇ¿ÉÒÔÖØд IlluminateFoundationAuthAuthenticatesUsers trait ÖÐµÄ authenticated() ÒªÁ죺

use IlluminateSupportFacadesAuth;

class UserController extends Controller
{
    use AuthenticatesUsers;

    protected function authenticated(Request $request, $user)
    {
        return redirect()->route('user.show', $user->id);
    }
}

µÇ¼ºó¸´ÖÆ

ÉÏÊö´úÂë¿ÉÒÔÔÚÓû§µÇ¼Àֳɺó £¬Ìøתµ½Ö¸¶¨µÄÓû§Ð¡ÎÒ˽ÈË×ÊÁÏÒ³¡£

Öض¨Ïòµ½ÏÈÇ°Ò³Ãæ

ÓÐʱ¼ä £¬ÎÒÃÇÐèÒª½«Óû§µÇ¼ºóµÄÌøת·¾¶ÉèÖÃΪµÇ¼֮ǰµÄÒ³Ãæ £¬¿ÉÒÔʹÓà Laravel ÌṩµÄ session() º¯ÊýºÍ URL::previous() ÒªÁì¡£

ÀýÈç £¬ÔڵǼ¿ØÖÆÆ÷ÖÐ £¬ÎÒÃÇ¿ÉÒÔÕâÑùʵÏÖ£º

use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;
use IlluminateSupportFacadesURL;

class LoginController extends Controller
{
    public function login(Request $request)
    {
        $credentials = $request->only('email', 'password');
        if (Auth::attempt($credentials)) {
            return redirect()->intended(URL::previous());
        }
        return back()->withErrors(['email' => 'µÇ¼ʧ°Ü']);
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÊö´úÂëÖÐ £¬ÎÒÃÇʹÓÃÁË redirect()->intended() ÒªÁì £¬¸ÃÒªÌå»á½«Óû§Öض¨Ïòµ½µÇ¼֮ǰ»á¼ûµÄÒ³Ãæ¡£ÈôÊÇÓû§Ö®Ç°Ã»Óлá¼û¹ýÆäËûÒ³Ãæ £¬Ôò»áÖض¨ÏòÖÁĬÈϵĵǼÌøת·¾¶¡£

ʹÓÃÖÐÐļþÌøתµ½Ö¸¶¨Ò³Ãæ

Laravel ¿ò¼ÜÖÐÐļþÌṩÁ˱ã½ÝµÄÉí·ÝÈÏÖ¤ºÍÊÚȨ¹¦Ð§¡£ÎÒÃÇ¿ÉÒÔÔÚijÆäÖÐÐļþÖÐÖ¸¶¨µÇ¼ºóµÄÌøת·¾¶¡£

ÀýÈç £¬ÎÒÃÇ¿ÉÒÔÔÚ auth ÖÐÐļþÖÐÉèÖõǼºóµÄÌøת·¾¶£º

namespace AppHttpMiddleware;

use IlluminateAuthMiddlewareAuthenticate as Middleware;

class Authenticate extends Middleware
{
    protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            return route('login'); // ÉèÖÃĬÈϵÄÌøת·¾¶
        }
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÊö´úÂëÖÐ £¬ÎÒÃÇʹÓà redirectTo() ÒªÁìÀ´´¦ÀíµÇ¼ʧ°ÜµÄÇëÇó¡£ÈôÊÇÇëÇóʱÆÚÍû·µ»Ø json ÃûÌÃÊý¾Ý £¬ÔòÖ±½Ó·µ»Ø 401 ¹ýʧ£»²»È» £¬½«Óû§Öض¨Ïòµ½µÇ¼ҳÃæ¡£

ÈôÊÇÐèÒªÖ¸¶¨ÆäËûµÄÌøת·¾¶ £¬Ö»ÐèÒªÐÞ¸Ä return Óï¾äÖеÄ·ÓÉÓÖÃû¼´¿É¡£

×ܽá

ÒÔÉϾÍÊÇÔÚ Laravel ¿ò¼ÜÖÐʵÏÖÓû§µÇ¼ºóÌøתµÄ¼¸ÖÖ·½·¨¡£ÏêϸѡÔñÄÄÖÖ·½·¨ £¬È¡¾öÓÚ¿ª·¢ÕßµÄÏÖʵÐèÇóºÍ¿ª·¢³¡¾°¡£ÎÞÂÛÊÇÄÄÖÖ·½·¨ £¬¶¼¿ÉÒÔÀû±ã¿ì½ÝµØ×ÊÖúÎÒÃÇʵÏÖÓû§ÈÏÖ¤¹¦Ð§ºÍµÇ¼ºóµÄÌøת¡£

ÒÔÉϾÍÊÇlaravel ÉÏ°¶ºóÌøתµÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是博ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是博ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是博

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
sitemap¡¢ÍøÕ¾µØͼ