Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

php - 在Laravel 5中使用URL中的参数重定向::: route(Redirect::route with parameter in URL in Laravel 5)

I'm developing a Laravel 5 app, I have this route

(我正在开发Laravel 5应用,我有这条路线)

Route::get('states/{id}/regions', ['as' => 'regions', 'uses' => 'RegionController@index']);

In my controller, after I make a post call correctly, I want to redirect to that view, with this command:

(在我的控制器中,正确地进行post调用后,我想使用以下命令重定向到该视图:)

return Redirect::route('regions')->with('message', 'State saved correctly!!!');

The problem is that I don't know how can I pass {id} parameter, which should be in my URL.

(问题是我不知道如何传递{id}参数,该参数应该在我的URL中。)

Thank you.

(谢谢。)

  ask by Bellots translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can pass the route parameters as second argument to route() :

(您可以将route参数作为第二个参数传递给route() :)

return Redirect::route('regions', [$id])->with('message', 'State saved correctly!!!');

If it's only one you also don't need to write it as array:

(如果只有一个,则也不需要将其写为数组:)

return Redirect::route('regions', $id)->with('message', 'State saved correctly!!!');

In case your route has more parameters, or if it has only one, but you want to clearly specify which parameter has each value (for readability purposes), you can always do this:

(如果您的路由有更多参数,或者只有一个参数,但是您想清楚地指定哪个参数具有每个值(出于可读性目的),则始终可以执行以下操作:)

return Redirect::route('regions', ['id'=>$id,'OTHER_PARAM'=>'XXX',...])->with('message', 'State saved correctly!!!');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...