devnull Ψ
devnull Ψ

Reputation: 4029

add get parameter to laravel's redirect method

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

Upvotes: 9

Views: 39279

Answers (6)

Fred Maclean
Fred Maclean

Reputation: 71

Its best to do it this way in your case:

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

Upvotes: 2

Sujan Gainju
Sujan Gainju

Reputation: 4769

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

Upvotes: 15

Babak Asadzadeh
Babak Asadzadeh

Reputation: 1239

just do something like this:

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

Upvotes: 5

FULL STACK DEV
FULL STACK DEV

Reputation: 15941

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

Upvotes: 1

DsRaj
DsRaj

Reputation: 2328

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

Upvotes: 3

Phiter
Phiter

Reputation: 14992

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

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

Upvotes: 6

Related Questions