Reputation: 1189
I am working on Laravel 5.4.36 application where my route label is defined in web.php as,
Route::get('/label/', 'LabelController@index');
but when I am trying to redirect from a control function to a route label using,
return redirect()->route('label');
Error: InvalidArgumentException Route [label] not defined.
or
return Redirect::to('label');
Error: FatalErrorException Class 'App\Http\Controllers\Redirect' not found.
both are not not working, Can anyone help me how to redirect to a route in Laravel?
Upvotes: 1
Views: 13555
Reputation: 456
use
Route::get('/label', 'LabelController@index')->name('label');
instead of
Route::get('/label/', 'LabelController@index');
remove / after label. no need of / at the end of route if you are not passing any parameters
Upvotes: 0
Reputation: 4035
So there are many ways to do it but i prefer below two ways for redirect.
1) With out defining name of the route:
Route::get('/label', 'LabelController@index');
return redirect('/label');
2) by Defining the name of the route:
Route::get('/label', 'LabelController@index')->name('label');
return redirect()->route('label');
Upvotes: 0
Reputation: 767
In your routes/web.php:
Route::get('/label/', 'LabelController@index')->name('label');
In your LabelController, at the end of your function index:
return redirect()->route('label');
Upvotes: 0
Reputation:
route()
redirects to a named route, so you need to name your route in your routes/web.php
:
Route::name('label')->get('/label/', 'LabelController@index');
https://laravel.com/docs/5.4/routing
Upvotes: 1
Reputation: 504
You can rewrite route to Route::get('label', 'LabelController@index');
and then call return redirect()->route('label');
OR
rewrite redirection call to return redirect()->route('/label/');
Please try this
Upvotes: 0