Home > database >  Create redirect with parameter in Laravel routes
Create redirect with parameter in Laravel routes

Time:01-12

I have this route in my web.php:

Route::get('/fr/action/{ticker}', 'AnalysisController@index')->name('analysis.slug.French');

I want to create a 301 redirect from this page to '/stock/{ticker}' but I'm not sure how to pass the $ticker parameter to the redirect here.

This doesn't work:

Route::get('/fr/action/{ticker}', function() { return redirect()->to('stock/{ticker}') });

CodePudding user response:

I am not sure but this might work;

Route::get('/fr/action/{ticker}', function($ticker) {
    return redirect()->to('stock/'.$ticker);
});

But better solution from official docs, you can create a route and redirect named route with parameter.


EDIT:

Route::get('/fr/action/{ticker}', function($ticker) {
    return redirect()->route('yourDefinedStockRoute', ['ticker' => $ticker]);
});

yourDefinedStockRoute is the name of "stock/{ticker}" route

CodePudding user response:

You can use Route::redirect to do this:

Route::redirect('/fr/action/{ticker}', '/stock/{ticker}', 301);

Laravel 8.x Docs - Routing - Redirect Routes Route::redirect

  •  Tags:  
  • Related