laravel·¾¶Ìøת
ÔÚ¿ª·¢webÓ¦ÓóÌÐòʱ£¬Ðí¶àʱ¼äÐèÒª¾ÙÐÐÒ³ÃæÌøת¡£laravel¿ò¼ÜÌṩÁ˶àÖÖ·½·¨¾ÙÐÐÌøת£¬±¾ÎÄÕ½«ÏÈÈÝlaravelÖз¾¶ÌøתµÄ¼¸ÖÖ·½·¨¡£
RedirectÀà
ʹÓÃLaravelµÄRedirectÀà¿ÉÒÔÀû±ãµØ¾ÙÐз¾¶Ìøת¡£¸ÃÀà°üÀ¨¶à¸ö³£ÓõÄÌøת·½·¨£¬ÀýÈçÌøתµ½ÁíÒ»¸öURL¡¢Ìøת»ØÉÏÒ»¸öÒ³ÃæµÈ¡£ÒÔÏÂÊÇһЩ³£ÓÃÒªÁìµÄʾÀý£º
//Ìøתµ½Ö¸¶¨µÄURL return redirect('/user/profile'); //Ìøת»ØÉÏÒ»¸öURL return back(); //ת´ïÊý¾Ý²¢Ìøתµ½Ö¸¶¨µÄ·ÓÉ return redirect()->route('profile', ['id' => 1]); //Ìøתµ½Ö¸¶¨¿ØÖÆÆ÷µÄÒªÁì return redirect()->action('UserController@index');
µÇ¼ºó¸´ÖÆ
ÃüÃû·ÓÉ
ͨ¹ýΪ·Óɽç˵Ãû³Æ£¬¿ÉÒÔʹµÃÔÚÌøתʱ²»ÐèҪʹÓÃÓ²±àÂëµÄURLµØµã¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔÔÚ·Óɽç˵ÖÐΪÃûΪ”home”µÄ·ÓÉÖ¸ÃüÃû³Æ£¬ÈçÏÂËùʾ£º
Route::get('/', function () { return view('home'); })->name('home');
µÇ¼ºó¸´ÖÆ
ÔÚ´úÂëÖУ¬ÎÒÃÇͨ¹ý->name(‘home’)Ö¸¶¨ÁËÃûΪ”home”µÄ·ÓÉ¡£ÕâÑù£¬ÎÒÃÇÔÚ¾ÙÐÐÌøתʱ¾Í¿ÉÒÔʹÓøÃÃû³Æ£¬¶øÎÞÐèÓ²±àÂëURLµØµã¡£ÈçÏÂËùʾ£º
//Ö±½ÓʹÓ÷ÓÉÃû³Æ¾ÙÐÐÌøת return redirect()->route('home');
µÇ¼ºó¸´ÖÆ
URLÌìÉúÆ÷
LaravelÖеÄURLÌìÉúÆ÷¿ÉÒÔÀû±ãµØÌìÉúÖ¸¶¨Â·ÓÉ»òURLµØµãµÄ×Ö·û´®¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓÃroute()ÒªÁìÌìÉúÃûΪ”home”µÄ·ÓɵÄURLµØµã£º
//ÌìÉúÃûΪ"home"µÄ·ÓɵÄURLµØµã $url = route('home');
µÇ¼ºó¸´ÖÆ
³ý´ËÖ®Í⣬ÎÒÃÇ»¹¿ÉÒÔʹÓÃurl()ÒªÁìÌìÉúÖ¸¶¨URLµØµãµÄ×Ö·û´®£º
//ÌìÉúÖ¸¶¨URLµØµãµÄ×Ö·û´® $url = url('/user/profile');
µÇ¼ºó¸´ÖÆ
JavaScriptÌøת
ÔÚijЩÇéÐÎÏ£¬Ê¹ÓÃJavaScript¾ÙÐÐÒ³ÃæÌøת¿ÉÄÜÔ½·¢ºÏÊÊ¡£Laravel¿ò¼ÜÌṩÁËÀû±ãµÄJavaScriptÌøתҪÁ죬ÈçÏÂËùʾ£º
//ÔÚJavaScriptÖоÙÐз¾¶Ìøת return redirect()->away('http://www.example.com');
µÇ¼ºó¸´ÖÆ
ÐèҪעÖصÄÊÇ£¬ÕâÖÖÒªÁìÐèÒª¾ÙÐÐÌØÁíÍâ´¦Àí£¬ÒÔ×èÖ¹¿çÕ¾¾ç±¾¹¥»÷£¨XSS£©¡£
×ܽá
LaravelÌṩÁ˶àÖÖ¾ÙÐз¾¶ÌøתµÄÒªÁ죬¿ª·¢Õß¿ÉÒÔƾ֤ÐèÇóÑ¡ÔñÊʺϵķ½·¨¡£Ê¹ÓÃRedirectÀà¿ÉÒÔÀû±ãµØ¾ÙÐÐÌøת£¬Í¨¹ýÃüÃû·ÓÉ¿ÉÒÔ×èÖ¹Ó²±àÂëURLµØµã£¬Ê¹ÓÃURLÌìÉúÆ÷¿ÉÒÔÀû±ãµØÌìÉúÖ¸¶¨Â·ÓÉ»òURLµØµãµÄ×Ö·û´®£¬¶øJavaScriptÌøתÔò¿ÉÒÔÖª×ãÔÚijЩÇéÐÎÏÂÐèҪʹÓÃJavaScript¾ÙÐÐÌøתµÄÐèÇ󡣿ª·¢Õß¿ÉÒÔÍŽáÏÖʵÐèÇóÀ´ÎÞаÔËÓÃÕâЩҪÁ죬ÌáÉý¿ª·¢Ð§ÂʺÍÓû§ÌåÑé¡£
ÒÔÉϾÍÊÇlaravel·¾¶ÌøתµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡