12
votes

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?

3
try Redirect::to('/manager')Mostafa Elgaafary
i tried that, but then it's just returning to the page i already am onTobias Hagenbeek

3 Answers

17
votes

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.

1
votes

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

-2
votes

use return Redirect::intended('mannage');