Bel*_*ots 16 php routes laravel
我正在开发一个Laravel 5应用程序,我有这条路线
Route::get('states/{id}/regions', ['as' => 'regions', 'uses' => 'RegionController@index']);
在我的控制器中,在我正确地进行调用后,我想使用以下命令重定向到该视图:
return \Redirect::route('regions')->with('message', 'State saved correctly!!!');
Run Code Online (Sandbox Code Playgroud)
问题是我不知道如何传递{id}参数,该参数应该在我的URL中.
谢谢.
luk*_*ter 21
您可以将路由参数作为第二个参数传递给route():
return \Redirect::route('regions', [$id])->with('message', 'State saved correctly!!!');
Run Code Online (Sandbox Code Playgroud)
如果它只是一个你也不需要把它写成数组:
return \Redirect::route('regions', $id)->with('message', 'State saved correctly!!!');
Run Code Online (Sandbox Code Playgroud)
如果您的路线有更多参数,或者它只有一个参数,但您想清楚地指定哪个参数具有每个值(为了便于阅读),您始终可以这样做:
return \Redirect::route('regions', ['id'=>$id,'OTHER_PARAM'=>'XXX',...])->with('message', 'State saved correctly!!!');
Run Code Online (Sandbox Code Playgroud)
你仍然可以这样做:
return redirect()->route('regions', $id)->with('message', 'State saved correctly!!!');
Run Code Online (Sandbox Code Playgroud)
如果您有多个参数,可以将参数作为数组传递,例如,假设您必须传递路线中特定区域的大写,您的路线可能如下所示:
Route::get('states/{id}/regions/{capital}', ['as' => 'regions', 'uses' => 'RegionController@index']);
Run Code Online (Sandbox Code Playgroud)
然后你可以使用以下方法重定向:
return redirect()->route('regions', ['id' = $id, 'capital' => $capital])->with('message', 'State saved correctly!!!');
Run Code Online (Sandbox Code Playgroud)