Ahmed Syed
Ahmed Syed

Reputation: 1189

Redirect to route in laravel not working

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

Answers (5)

Saeed Awan
Saeed Awan

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

Salman Zafar
Salman Zafar

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

Shreeraj
Shreeraj

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

user8034901
user8034901

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

Arya
Arya

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

Related Questions