duanjia9577
duanjia9577
2018-06-12 12:04
浏览 75
已采纳

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

I use laravel 5.6

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');
});

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

UPDATE

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • drfm55597
    drfm55597 2018-06-12 12:11
    已采纳

    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])
    });
    

    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])
    });
    
    点赞 评论
  • dongwen9975
    dongwen9975 2018-06-12 12:08

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

    return redirect()->route('en.about', ['param' => 123]);
    
    点赞 评论
  • dsjbest2015
    dsjbest2015 2018-06-12 12:12
    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.

    Hope this helps

    点赞 评论
  • doupengxie4195
    doupengxie4195 2018-06-12 12:18

    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');
    
    点赞 评论
  • dongmengan8620
    dongmengan8620 2018-06-12 12:31

    just do something like this:

     return redirect('/en/about?param='.$param);
    
    点赞 评论
  • donglipi4495
    donglipi4495 2019-02-24 23:25

    Its best to do it this way in your case:

    return redirect(route("en.about")."?param=123");
    
    点赞 评论

相关推荐