Laravel 4:无法为命名路由“登录”生成 URL,因为此类路由不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15163289/
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: Unable to generate a URL for the named route "login" as such route does not exist
提问by sehummel
I'm creating an authorization system in my Laravel 4 project. I am trying to use the auth "before" filter.
我正在我的 Laravel 4 项目中创建一个授权系统。我正在尝试使用 auth “before” 过滤器。
In my routes.php
file, I have:
在我的routes.php
文件中,我有:
Route::get('viewer', array('before' => 'auth', function() {
return View::make('lead_viewer');
}));
Route::get('login', 'LoginController');
The before filter calls this line in the filters.php
file:
before 过滤器调用filters.php
文件中的这一行:
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::route('login');
});
I can manually navigate to my login route. But the auth system isn't letting this happen. I've run composer dump-autoload
a couple of times, so that isn't the problem. What am I doing, since I can actually load the login page if I do it manually?
我可以手动导航到我的登录路线。但是身份验证系统不会让这种情况发生。我跑composer dump-autoload
了几次,所以这不是问题。我在做什么,因为如果我手动加载,我实际上可以加载登录页面?
回答by sehummel
I figure it out. Laravel is looking for a named route: I had to do this:
我想通了。Laravel 正在寻找一条命名路线:我必须这样做:
Route::get('login', array('as' => 'login', function() {
return View::make('login');
}));
An interesting, not very intuitive approach in Laravel. But there must be a reason Taylor did this that I'm not seeing.
Laravel 中一种有趣但不是很直观的方法。但是泰勒这样做肯定有我没有看到的原因。
回答by Dustin Fraker
To do what you were trying to do in your initial approach you could have just done:
要完成您在最初的方法中尝试做的事情,您可以这样做:
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::to('/login');
});
and it would have worked just fine.
它会工作得很好。
If you want to use named routes then you do what you posted in your answer to your own question. Essentially...more than one way to skin a cat.
如果您想使用命名路线,那么您可以按照您在自己问题的回答中发布的内容进行操作。本质上……不止一种给猫剥皮的方法。
Hope that helps
希望有帮助
回答by tafftoo
I know you've probably solved this by now but after stumbling across your post while trying to solve a similar problem, I wanted to share my thoughts...
我知道您现在可能已经解决了这个问题,但是在尝试解决类似问题时偶然发现您的帖子后,我想分享我的想法......
Laravel is NOTlooking for a named route for the guest method, it is expecting a path.
Laravel不是在为来宾方法寻找命名路由,它期待的是path。
Your example works because because the named route and the path are the same i.e. "login". Try changing your URL to something other than 'login' and watch it fail.
您的示例有效,因为命名路由和路径相同,即“登录”。尝试将您的 URL 更改为“登录”以外的其他内容并观察它是否失败。
If you want to use a named route you should use the route helper method as so...
如果你想使用命名路由,你应该使用路由助手方法......
if (Auth::guest()) return Redirect::guest( route('login') )
Hope that helps someone.
希望能帮助某人。