Laravel 4 - 重定向时未定义路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18804356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4 - route is not defined, on redirect
提问by Tobias Hagenbeek
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.
如果我去http://example.com/manage,路线工作正常..laravel 的标志在那里,我的另一个页面也很好。
But when i do:
但是当我这样做时:
Redirect::route('/manage');
the page dies saying:
页面死了说:
Route [/manage] not defined
Anybody have an idea?
有人有想法吗?
回答by The Alpha
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.
您应该在使用Redirect::route
方法时使用路由名称,在这种情况下,您必须使用名称声明路由,即
Route::any('/manage', array('as' => 'manage', 'uses' => 'AdminController@showWelcome'));
Here, as
value is name of the route
, so, now you can use
在这里,as
值是 的名称route
,因此,现在您可以使用
return Redirect::route('manage'); // 'manage' is the name of the route to redirect
Or, alternatively, you can use Redirect::to('url')
method, i.e.
或者,您也可以使用Redirect::to('url')
方法,即
return Redirect::to('/manage'); // '/manage' is the url to redirect
Check Redirect to a named Routeand named routes.
回答by Aniruddha Shevle
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'.
在下面用'as' => 'route_name'定义的数组中定义一个路由名称“manage”,如下所示。
Solution :
解决方案 :
Route::any('manage', [
'as' => 'manage',
'uses' => 'AdminController@showWelcome'
]);
Please refer the link : https://laravel.com/docs/master/routing#named-routes
回答by Nikhil
use return Redirect::intended('mannage');
使用 return Redirect::intended('manage');