Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 - route is not defined, on redirect

I'm trying to setup a simple redirect after a login. The logging in part works but the redirect fails because it says the route doesn't exist.

This is my routes file:

Route::any('/', array('uses' => 'UsersController@login'));

Route::any('/manage', array('uses' => 'AdminController@showWelcome'));

And the route works fine if i go to http://example.com/manage .. the logo of laravel is there, and my other page is fine as well.

But when i do:

Redirect::route('/manage');

the page dies saying:

Route [/manage] not defined

Anybody have an idea?

like image 637
Tobias Hagenbeek Avatar asked Sep 14 '13 17:09

Tobias Hagenbeek


2 Answers

You should use the route name when you are using Redirect::route method and in this case you have to declare the route using a name, i.e.

Route::any('/manage', array('as' => 'manage', 'uses' => 'AdminController@showWelcome'));

Here, as value is name of the route, so, now you can use

return Redirect::route('manage'); // 'manage' is the name of the route to redirect

Or, alternatively, you can use Redirect::to('url') method, i.e.

return Redirect::to('/manage'); // '/manage' is the url to redirect

Check Redirect to a named Route and named routes.

like image 151
The Alpha Avatar answered Sep 19 '22 13:09

The Alpha


This error "Route [manage] not defined" is because of the route name "manage" is not defined.

Route name and Route path are two different things.

And you've declared the route path as admin,

Route::any('manage', 'AdminController@showWelcome');

However,

return redirect()->route('manage');

means that you are redirecting the flow to the route named "manage".

To sort the error,

Define a route name "manage" as follows in an array defined below with 'as' => 'route_name'.

Solution :

Route::any('manage', [
   'as' => 'manage',
   'uses' => 'AdminController@showWelcome'
]);

Please refer the link : https://laravel.com/docs/master/routing#named-routes

like image 39
Aniruddha Shevle Avatar answered Sep 20 '22 13:09

Aniruddha Shevle