8

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 Answers 6

15

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])
});
4
  • thanks for that, but in this case I have to create named route at first
    – devnull Ψ
    Jun 12, 2018 at 12:16
  • 1
    for some reasons it doesnt see the parameter. I added it like /about/{param?}, function($param=null) because its optional, but I cannot get value
    – devnull Ψ
    Jun 12, 2018 at 12:28
  • i have update the answer. Have you tried that route with and without passing a parameter? Jun 12, 2018 at 12:37
  • not sure if it something to with version but I need the parameter to be passed to the function call. ie function ($param)
    – matt
    May 25, 2021 at 11:08
6

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

return redirect()->route('en.about', ['param' => 123]);
2
  • thanks, but again it requires the named route. is there way to do it without creating the named route?
    – devnull Ψ
    Jun 12, 2018 at 12:17
  • 2
    Not sure, why can't you create a named route? There are many benefits from using them.
    – Phiter
    Jun 12, 2018 at 12:19
5

just do something like this:

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

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');
2
  • thanks, but the issue is that the project which I work with mostly just returns the view, it doesn't call to controllers method
    – devnull Ψ
    Jun 12, 2018 at 12:19
  • but for redirect, you need to register that route
    – DsRaj
    Jun 12, 2018 at 12:21
2

Its best to do it this way in your case:

return redirect(route("en.about")."?param=123");
1
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

2
  • it fails, method url does not exist . maybe it was in older laravel's version
    – devnull Ψ
    Jun 12, 2018 at 12:15
  • @devnullΨ have a look at the updated answer I had a typo Jun 12, 2018 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.