laravelÔõÑùʵÏÖÌøתҳÃæ´ø²ÎÊýµÄ¹¦Ð§
ÔÚlaravel¿ò¼ÜÖУ¬ÌøתҳÃæÊǷǾ³£¼ûµÄ²Ù×÷¡£ÓÐʱ¼äÐèÒªÔÚÌøתʱ´øһЩ²ÎÊý£¬ºÃ±ÈËÑË÷Òªº¦´Ê¡¢·ÖÒ³ÊýµÈµÈ¡£Õâʱ¼ä¾ÍÐèҪʵÏÖÌøתҳÃæ´ø²ÎÊýµÄ¹¦Ð§¡£±¾ÎĽ«ÏÈÈÝÔõÑùʵÏÖÌøתҳÃæ´ø²ÎÊýµÄ¹¦Ð§£¬ÊµÏÖÎÞÖض¨Ïò´«²Î¡£
1¡¢»ù±¾Ìøת
ÔÚLaravel¿ò¼ÜÖУ¬ÌøתÓÐÁ½ÖÖ·½·¨£¬Ò»ÖÖÊÇʹÓÃredirectº¯Êý£¬ÁíÒ»ÖÖÊÇʹÓÃviewº¯Êý¡£redirectº¯ÊýÓÃÓÚÌøתµ½ÆäËû¿ØÖÆÆ÷µÄÒªÁ죬viewº¯ÊýÓÃÓÚÌøתµ½ÊÓͼÎļþ¡£
ʹÓÃredirectº¯Êý£¬¿ÉÒÔÕâÑùд£º
return redirect()->action( '[¿ØÖÆÆ÷Ãû³Æ]@[ÒªÁìÃû³Æ]' );
µÇ¼ºó¸´ÖÆ
ʹÓÃviewº¯Êý£¬¿ÉÒÔÕâÑùд£º
return view('[ÊÓͼÎļþ·¾¶]', [²ÎÊýÊý×é]);
µÇ¼ºó¸´ÖÆ
ÕâÁ½ÖÖ·½·¨¶¼Ã»ÓÐת´ï²ÎÊý£¬ÈôÊÇÐèҪת´ï²ÎÊý£¬ÎÒÃÇÐèÒª½ÓÄÉÒÔÏÂÒªÁì¡£
2¡¢´ø²ÎÊýÌøת
2.1¡¢Ê¹ÓÃÖÐÑë¼þ
LaravelÌṩÁ˺ÜÊÇÀû±ãµÄÖÐÑë¼þ¹¦Ð§£¬ÎÒÃÇ¿ÉÒÔʹÓÃÖÐÑë¼þ½«²ÎÊýת´ïµ½ÏÂÒ»¸öÇëÇóÖС£
ÎÒÃÇ¿ÉÒÔн¨Ò»ÆäÖÐÑë¼þ£¬ÔÚÖÐÑë¼þÖн«²ÎÊýÉúÑÄÆðÀ´£¬È»ºóÔÚÏÂÒ»¸öÇëÇóÖÐÈ¡³öÕâЩ²ÎÊý¡£ÖÐÑë¼þ´úÂëÈçÏ£º
<?php namespace App\Http\Middleware; use Closure; class ParamsMiddleware { public function handle($request, Closure $next) { $params = [ 'param1' => 'value1', 'param2' => 'value2', 'param3' => 'value3', ]; $request->session()->put('params', $params); return $next($request); } }
µÇ¼ºó¸´ÖÆ
½«²ÎÊýÉúÑÄÔÚsessionÖУ¬È»ºóÔÚÏÂÒ»¸öÇëÇóÖÐÈ¡³öÀ´¡£
$params = $request->session()->get('params');
µÇ¼ºó¸´ÖÆ
ÎÒÃÇ¿ÉÒÔÔÚweb.phpÎļþÖÐ×¢²áÖÐÑë¼þ£¬²¢½«ÖÐÑë¼þÌí¼Óµ½Â·ÓÉÖС£
Route::middleware('params')->get('/', '[¿ØÖÆÆ÷Ãû³Æ]@[ÒªÁìÃû³Æ]');
µÇ¼ºó¸´ÖÆ
ÕâÖÖ·½·¨µÄÈõµãÊÇÐèҪʹÓÃsession£¬ÈôÊÇûÓпªÆôsession£¬ÔòÎÞ·¨Ê¹Óá£
2.2¡¢Ê¹ÓÃURL²ÎÊý
ÎÒÃÇ»¹¿ÉÒÔʹÓÃURL²ÎÊý½«²ÎÊýת´ïµ½ÏÂÒ»¸öÒ³ÃæÖС£
ÔÚÌøתʱ£¬ÎÒÃÇ¿ÉÒÔʹÓÃrouteº¯ÊýÖ¸¶¨Â·ÓÉ£¬È»ºóÔÚ·ÓÉÖÐÉèÖòÎÊý¡£
Ìøת´úÂ룺
return redirect()->route('[·ÓÉÃû³Æ]', ['param1' => 'value1', 'param2' => 'value2']);
µÇ¼ºó¸´ÖÆ
·ÓÉ´úÂ룺
Route::get('/', '[¿ØÖÆÆ÷Ãû³Æ]@[ÒªÁìÃû³Æ]')->name('[·ÓÉÃû³Æ]');
µÇ¼ºó¸´ÖÆ
ÔÚ¿ØÖÆÆ÷ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃrequest¹¤¾ß»ñÈ¡URL²ÎÊý¡£
$request->input('param1');
µÇ¼ºó¸´ÖÆ
ÕâÖÖ·½·¨µÄÈõµãÊÇURL²ÎÊý»áÏÔʾÔÚURLµØµãÀ¸ÖУ¬¿ÉÄÜ»á¶ÔÓû§Ôì³ÉÒ»¶¨µÄÀ§ÈÅ¡£
2.3¡¢Ê¹ÓÃÖض¨Ïò²ÎÊý
LaravelÌṩÁËÁíÒ»ÖÖÒªÁ죬¿ÉÒÔʵÏÖÎÞÖض¨Ïò´«²Î¡£ÎÒÃÇÏÈ¿´´úÂ룬ȻºóÔÚÚ¹ÊÍ¡£
return redirect()->to('/')->with('param1', 'value1')->with('param2', 'value2');
µÇ¼ºó¸´ÖÆ
ÔÚÖض¨Ïòʱ£¬ÎÒÃÇ¿ÉÒÔʹÓÃwithº¯Êý½«²ÎÊýÉúÑÄÔÚsessionÖС£ÕâÑù£¬²ÎÊý¾Í¿ÉÒÔÔÚÏÂÒ»¸öÇëÇóÖÐʹÓÃÁË¡£
ÔÚ¿ØÖÆÆ÷ÖУ¬ÎÒÃÇ¿ÉÒÔÕâÑù»ñÈ¡²ÎÊý¡£
$request->session()->get('param1'); $request->session()->get('param2');
µÇ¼ºó¸´ÖÆ
ÕâÖÖ·½·¨ÊµÏÖÁËÎÞÖض¨Ïò´«²Î£¬Í¬Ê±Ò²×èÖ¹ÁËURL²ÎÊý¶ÔÓû§µÄ×ÌÈÅ¡£
×ܽá
ÔÚLaravelÖУ¬ÊµÏÖÌøתҳÃæ´ø²ÎÊýµÄ¹¦Ð§ÓжàÖÖÒªÁì¡£¿ÉÒÔʹÓÃÖÐÑë¼þ¡¢URL²ÎÊý»òÕßÖض¨Ïò²ÎÊý¡£²î±ðµÄÒªÁìÓвî±ðµÄÓÅÈõµã£¬ÎÒÃÇÐèҪƾ֤ÏÖʵÐèÇóÑ¡Ôñ×îºÏÊʵÄÒªÁì¡£
ÒÔÉϾÍÊÇlaravelÔõÑùʵÏÖÌøתҳÃæ´ø²ÎÊýµÄ¹¦Ð§µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡