简体   繁体   English

Laravel 4.2路由过滤器根URI的auth / guest错误

[英]Laravel 4.2 routing filters auth/guest error for root URI

I'm attempting to route a single URI to multiple controllers, based on user authentication. 我正在尝试根据用户身份验证将单个URI路由到多个控制器。 Essentially, if the user is not logged in and they hit the root URI, show a generic landing page, otherwise, if they are logged in, and access the root URI, show their personalized content. 本质上,如果用户未登录并点击了根URI,则显示一个通用的登录页面;否则,如果用户登录并访问了根URI,则显示其个性化内容。

I am using the standard out-of-the-box filters (auth/guest) and some other routes (not shown here) that have been setup to quickly auth/de-auth for testing. 我使用的是标准的现成过滤器(身份验证/来宾)和其他一些路由(此处未显示),它们已设置为快速进行身份验证/取消身份验证以进行测试。

The idea seems straight-forward enough and seems to me like it should work, yet Laravel is not handling this correctly: 这个想法似乎很简单,在我看来它应该可行,但是Laravel没有正确处理这个问题:

Route::group(array('before' => 'auth'), function() {
    Route::get('/', function() { echo 'logged in'; });
});

Route::group(array('before' => 'guest'), function() {
    Route::get('/', function() { echo 'logged out'; });
});

It doesn't matter what order I have these in, Laravel will not acknowledge the Auth filter when the user is authenticated. 我按这些顺序排列都无所谓,当用户通过身份验证时,Laravel不会确认Auth过滤器。 The first route gets skipped over and the Guest filter is running first, or solely (probably more accurately). 跳过第一条路线,并且Guest过滤器首先运行,或者单独运行(可能更准确)。

Did I mistakenly change something in one of the filters? 我是否错误地更改了其中一个过滤器中的某些内容? Why is this happening? 为什么会这样呢? Shouldn't this work without a hitch? 这项工作不应该顺利吗?

It seems as if Laravel cannot handle the assignment of multiple actions to test for to a single URI. Laravel似乎无法处理要对单个URI进行测试的多个操作的分配。 I don't particularly want to spend time digging through the codebase to figure out the problem. 我不想特别花时间在代码库中进行挖掘以找出问题所在。 This seems to me to be a bad design decision with the framework itself, though if that is the case it would explain the problem here. 在我看来,对于框架本身来说,这是一个错误的设计决策,尽管如果是这样的话,它将在这里解释问题。

I need a sanity check, please. 请给我一个健康检查。

It should be this 应该是这个

Route::group(array('before' => 'guest|auth'), function() {
    Route::get('/', function() { 
        if(Auth::check()) {
            return "logged in";
        }

        return 'logged out';
    });
});

What is the point of multiple controller-action for one URI? 一个URI的多个控制器动作有什么意义? I'm pretty confused. 我很困惑。 An URI is served by one Controller 's action only. URI仅由一个Controller的操作提供。 Otherwise, it gets DRY , IMO. 否则,它将得到DRY ,IMO。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM