×ðÁú¿­Ê±¹ÙÍøµÇ¼

laravelÌøתµ½ÉÏÒ»¸öÒ³Ãæ

ÔÚʹÓÃlaravel¿ò¼Ü¾ÙÐÐweb¿ª·¢Ê±£¬ÎÒÃǾ­³£ÐèҪʵÏÖÒ³ÃæÖ®¼äµÄÌøת¡£ÕâЩÌøתÊÇͨ¹ý·ÓÉÀ´ÊµÏֵģ¬ÎÒÃÇ¿ÉÒÔÔÚ¿ØÖÆÆ÷ÖÐʹÓÃredirect()ÒªÁ콫ҳÃæÖض¨Ïòµ½Ö¸¶¨µÄurlÉÏ¡£ÓÐʱ¼äÎÒÃÇÐèҪʵÏÖÒ»¸ö¡°·µ»ØÉÏÒ»¸öÒ³Ã桱µÄ¹¦Ð§£¬±¾ÎĽ«ÏÈÈÝÔõÑùÔÚlaravelÖÐʵÏÖÕâ¸ö¹¦Ð§¡£

ÒªÁìÒ»£ºÊ¹ÓÃHTTP_REFERER

HTTP_REFERERÊÇHTTPЭÒéÖеÄÒ»¸öÍ·²¿×ֶΣ¬Ëü¼Í¼ÁË»á¼ûÄ¿½ñÒ³ÃæµÄÇ°Ò»¸öÒ³ÃæµÄURL¡£ÎÒÃÇ¿ÉÒÔͨ¹ý»á¼ûHTTP_REFERERÀ´ÊµÏÖ¡°·µ»ØÉÏÒ»¸öÒ³Ã桱µÄ¹¦Ð§¡£

Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚControllerÖÐʹÓÃback()ÒªÁ콫ҳÃæÖض¨Ïòµ½ÉÏÒ»¸öÒ³Ã棺

public function someAction(Request $request)
{
    // һЩ²Ù×÷
    return redirect()->back();
}

µÇ¼ºó¸´ÖÆ

back()ÒªÌå»á½«Ò³ÃæÖض¨Ïòµ½HTTP_REFERERÖ¸¶¨µÄURLÉÏ£¬´Ó¶øʵÏÖ¡°·µ»ØÉÏÒ»¸öÒ³Ã桱µÄ¹¦Ð§¡£

ÐèҪעÖصÄÊÇ£¬HTTP_REFERERÊÇÓÉ¿Í»§¶Ë·¢Ë͵ÄHTTPÍ·²¿×ֶΣ¬Òò´ËËüµÄÖµ¿ÉÄܱ»¸Ä¶¯»òÕß²»±£´æ¡£ÈôÊÇHTTP_REFERER²»±£´æ»òÕßÖµ²»¿É¿¿£¬ÎÒÃǾÍÐèҪʹÓÃÁíÍâµÄÒªÁìʵÏÖ¡°·µ»ØÉÏÒ»¸öÒ³Ã桱µÄ¹¦Ð§¡£

ÒªÁì¶þ£ºÊ¹ÓÃSession

Laravel¿ò¼ÜÖÐÌṩÁËÒ»¸öÃûΪsessionµÄЧÀÍ£¬ÎÒÃÇ¿ÉÒÔʹÓÃsessionÀ´´æ´¢¡°ÉÏÒ»¸öÒ³Ã桱µÄURL£¬²¢ÔÚÐèÒªµÄʱ¼ä¶ÁÈ¡Õâ¸öURLÀ´ÊµÏÖ¡°·µ»ØÉÏÒ»¸öÒ³Ã桱µÄ¹¦Ð§¡£

Ê×ÏÈ£¬ÔÚControllerÖн«Ä¿½ñÒ³ÃæµÄURL´æ´¢µ½sessionÖУº

public function someAction(Request $request)
{
    // һЩ²Ù×÷
    $previousUrl = url()->previous();
    $request->session()->put('previous_url', $previousUrl);
    return redirect()->route('some_other_route');
}

µÇ¼ºó¸´ÖÆ

ÕâÀïʹÓÃÁËurl()->previous()ÒªÁìÀ´»ñÈ¡Ä¿½ñÒ³ÃæµÄÉÏÒ»¸öÒ³ÃæµÄURL¡£È»ºóʹÓÃ$request->session()->put()ÒªÁ콫Õâ¸öURL´æ´¢µ½sessionÖС£

½ÓÏÂÀ´£¬ÔÚÐèÒª¡°·µ»ØÉÏÒ»¸öÒ³Ã桱µÄµØ·½£¬ÎÒÃÇ¿ÉÒÔ´ÓsessionÖлñȡ֮ǰ´æ´¢µÄURL£¬²¢½«Ò³ÃæÖض¨Ïòµ½Õâ¸öURLÉÏ£º

public function someOtherAction(Request $request)
{
    // һЩ²Ù×÷
    $previousUrl = $request->session()->get('previous_url');
    if (!empty($previousUrl)) {
        return redirect()->to($previousUrl);
    }
    // ÈôÊÇsessionÖÐûÓÐÉÏÒ»¸öÒ³ÃæµÄURL£¬¾Í·µ»ØÊ×Ò³
    return redirect()->route('home');
}

µÇ¼ºó¸´ÖÆ

ÕâÀïʹÓÃÁË$request->session()->get()ÒªÁì´ÓsessionÖлñȡ֮ǰ´æ´¢µÄURL¡£ÈôÊÇsessionÖб£´æÕâ¸öURL£¬¾ÍʹÓÃredirect()->to()ÒªÁ콫ҳÃæÖض¨Ïòµ½Õâ¸öURLÉÏ£¬´Ó¶øʵÏÖ¡°·µ»ØÉÏÒ»¸öÒ³Ã桱µÄ¹¦Ð§¡£

ÐèҪעÖصÄÊÇ£¬ÈôÊDz»¾ÙÐÐÏÞÖÆ£¬Ê¹ÓÃsession¿ÉÄܻᵼÖÂÇå¾²ÎÊÌâ¡£¹¥»÷Õß¿ÉÒÔαÔìsessionÖеÄÖµ£¬´Ó¶øʵÏÖ¿çÕ¾µãÇëÇóαÔ죨CSRF£©¹¥»÷¡£Òò´Ë£¬ÎÒÃÇÓ¦¸ÃÔÚʹÓÃsessionʱ¼ÓÈëһЩÇå¾²²½·¥£¬ºÃ±ÈʹÓÃtokenÀ´ÑéÖ¤ÇëÇóµÄÓÐÓÃÐÔ¡£

½áÂÛ

¡°·µ»ØÉÏÒ»¸öÒ³Ã桱ÊÇWebÓ¦ÓóÌÐòÖзǾ­³£¼ûµÄÒ»¸ö¹¦Ð§£¬Laravel¿ò¼ÜÌṩÁ˶àÖÖʵÏÖÕâ¸ö¹¦Ð§µÄÒªÁì¡£ÎÒÃÇ¿ÉÒÔʹÓÃHTTP_REFERERÍ·²¿×ֶλòÕßsessionÀ´ÊµÏÖÕâ¸ö¹¦Ð§¡£ÐèҪƾ֤ÏÖÕæÏàÐÎÑ¡Ôñ×îºÏÊʵÄÒªÁì¡£ÎÞÂÛÄÄÖÖÒªÁ죬¶¼ÐèҪעÖØÇå¾²²½·¥£¬±ÜÃâ¿çÕ¾µãÇëÇóαÔìµÈÇå¾²ÎÊÌâ¡£

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

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

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

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

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

QR code
ÍøÕ¾µØͼ