将 get 参数添加到 Laravel 的重定向方法中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50816769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:48:46  来源:igfitidea点击:

add get parameter to laravel's redirect method

phplaravellaravel-5

提问by devnull Ψ

I use laravel 5.6

我使用 Laravel 5.6

I have GETparameter which I want to pass to redirect function.

我有GET我想传递给重定向函数的参数。

Route::get('/about', function () {
   //I want to add param to this redirect function
   return redirect('/en/about');
});

if the route looks like /about?param=123after redirect the paramwill be lost. is there way to add parameter to redirect method? as I see this function doesn't include input parameters. the parameter is optional, so it may not be provided. maybe there's way to override this function? or some other solution? all suggestions will be appreciated

如果路由看起来像/about?param=123重定向后,param将会丢失。有没有办法向重定向方法添加参数?正如我所见,此函数不包含输入参数。该参数是可选的,因此可能不提供。也许有办法覆盖这个功能?或其他一些解决方案?所有建议将不胜感激

UPDATE

更新

is it possible to override the redirect()method ? I think in my case it will be the best solution

是否可以覆盖该redirect()方法?我认为就我而言,这将是最好的解决方案

回答by suzan

You have to get the parameter in the URL and pass it to redirect method in an array

您必须获取 URL 中的参数并将其传递给数组中的重定向方法

Route::get('/about/{param}', function () {
   return \Redirect::route('/en/about', ['param'=>$param])
});

without having to use named route

无需使用命名路由

Route::get('/about/{param}', function () {
   return redirect('/en/about', ['param'=>$param])
});

For optional parameter

对于可选参数

Route::get('/about/{param?}', function ($param = 'my param') {
   return redirect('/en/about', ['param'=>$param])
});

回答by DsRaj

If you don't want to add route name then you can do the same with controller function

如果您不想添加路由名称,则可以对控制器功能执行相同操作

Route::get('/about/{param}', function () {
   return \Redirect::action('CONTROLLER@FUNCTION',['param'=>$param])
});

OR with the helper function

或使用辅助函数

return redirect()->action('CONTROLLER@FUNCTION');

回答by Babak

just do something like this:

只是做这样的事情:

 return redirect('/en/about?param='.$param);

回答by Phiter

Yeah, you can redirect to named routesand pass parameters, like this:

是的,您可以重定向到命名路由并传递参数,如下所示:

return redirect()->route('en.about', ['param' => 123]);

回答by Adnan Mumtaz

Route::get('/about', function () {
   //I want to add param to this redirect function
   return redirect()->to(url('/en/about',['param' => 'Pram vakue', 'param2' => $param]));
});

If you use a route()then you have to create a named route.

如果您使用 aroute()那么您必须创建一个命名路由。

Hope this helps

希望这可以帮助

回答by Fred Maclean

Its best to do it this way in your case:

在你的情况下最好这样做:

return redirect(route("en.about")."?param=123");