Create redirect with parameter in Laravel routes
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}') });
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