将获取参数添加到laravel的重定向方法中 [英] add get parameter to laravel's redirect method
问题描述
我使用laravel 5.6
I use laravel 5.6
我有要传递给重定向功能的 GET
参数.
I have GET
parameter which I want to pass to redirect function.
Route::get('/about', function () {
//I want to add param to this redirect function
return redirect('/en/about');
});
如果路由在重定向后看起来像/about?param = 123
,则 param
将丢失.有没有办法将参数添加到重定向方法?如我所见,此函数不包含输入参数.该参数是可选的,因此可能不提供.也许有办法重写此功能?或其他解决方案?所有建议将不胜感激
if the route looks like /about?param=123
after redirect the param
will 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
更新
是否可以覆盖 redirect()
方法?我认为这将是最好的解决方案
is it possible to override the redirect()
method ? I think in my case it will be the best solution
推荐答案
您必须在URL中获取参数并将其传递给数组中的重定向方法
You have to get the parameter in the URL and pass it to redirect method in an array
Route::get('/about/{param}', function () {
return \Redirect::route('/en/about', ['param'=>$param])
});
无需使用命名路由
Route::get('/about/{param}', function () {
return redirect('/en/about', ['param'=>$param])
});
用于可选参数
Route::get('/about/{param?}', function ($param = 'my param') {
return redirect('/en/about', ['param'=>$param])
});
这篇关于将获取参数添加到laravel的重定向方法中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!