简体   繁体   中英

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. 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.

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:

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. The first route gets skipped over and the Guest filter is running first, or solely (probably more accurately).

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. 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? I'm pretty confused. An URI is served by one Controller 's action only. Otherwise, it gets DRY , IMO.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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