在Laravel 5中使用URL中的参数重定向:: route

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)

  • @Bellots在所示的示例中,您可以看到$ id作为第二个参数传递给route()方法。第二个参数是用于构建路线的参数。用`with()`方法添加的任何“参数”实际上只是将数据刷新到会话中,以便您在重定向时访问。因此,您的整行内容将是:'return \ Redirect :: route('regions',[$ id])-> with('message','状态正确保存!!!');` (2认同)
  • 试试这个`return redirect('states /'.$ id.'/ regions') - > with(['message'=>'State saved successfully !!!']);` (2认同)

Awa*_*ine 7

你仍然可以这样做:

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)