Home » Php » Laravel 4 – route is not defined, on redirect

Laravel 4 – route is not defined, on redirect

Posted by: admin October 29, 2017 Leave a comment

Questions:

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' => '[email protected]'));

Route::any('/manage', array('uses' => '[email protected]'));

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?

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' => '[email protected]'));

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.

Questions:
Answers:

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', '[email protected]');

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' => '[email protected]'
]);

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

Questions:
Answers:

use return Redirect::intended(‘mannage’);