5

I have recently got into developing with Laravel 4 and I had a question about routes.

For '/', I would like to have two different view pages based on the user's auth status.

If a user is logged in and is viewing '/', I would like to show them a view with admin controls and when a user is viewing '/' as a regular user without logging in, I would like to offer a general information view.

To accomplish this, I've been playing around with filter 'auth' and 'guest' but am having no luck. // app/routes.php

// route for logged in users
Route::get('/', array('before' => 'auth', function()
{
   return 'logged in!';
}));

// for normal users without auth
Route::get('/', function() 
{ 
    return 'not logged in!'; 
}));

The above code works to a point where the as a logged in user, I am able to display the proper response but after logging out, I cannot see the proper response as a regular user.

Perhaps this is something that should be handled in the controller? If someone could point me in the right direction, it would be really helpful.

tereško
  • 58,060
  • 25
  • 98
  • 150
Kevin Jung
  • 2,973
  • 7
  • 32
  • 35

1 Answers1

16

One (simple) option would be to use the Auth::check() function to see if they are logged in:

Route::get('/', function() 
{
    if (Auth::check())
    {
        return 'logged in!';
    }
    else
    {
        return 'not logged in!'; 
    }  
});

You would be able to use the same logic in the controller if you so wish.

EDIT - using filters

If you wanted to do this in the filter, you could use something like this:

Route::filter('auth', function()
{
    if (Auth::guest()) 
    {
        return Redirect::to('non-admin-home');
    }
});

and then defining a second route (or action in your controller) to handle the normal users. Though this would mean a different url for the page, which I don't think is what you want..

COMPLETE CONTROLLER-BASED ROUTING FLOW: (keeping routes.php clean)

routes.php

Route::controller('/', 'IndexController');

IndexController.php

class IndexController extends BaseController
{
    // HOME PAGE
    public function getIndex()
    {
        if (Auth::check())
        {
            return View::make('admin.home');
        }
        else
        {
            return View::make('user.home');
        }
    }
}
msturdy
  • 10,479
  • 11
  • 41
  • 52
  • Thanks for your input! This is the logical way to do it but I was unsure if putting if statements in the routes file was a standard way of doing things. I guess theres no rules against it if this is not doable with the built in route class. – Kevin Jung Aug 05 '13 at 03:31
  • yeah, I know what you mean.. though it is clean and easy to follow the logic. An alternative could be to use a filter to redirect the user to another view.. I'll update the above with something.. – msturdy Aug 05 '13 at 03:37
  • 2
    It's common practise to do what you want to do from within the view, as your only intention is to change what is displayed to the user. In such a case, you need not do your routing like the above. You simply need to apply the check from within a view. However, if you intend to change output controller logic that gets sent to the view before rendering it, then you would need to do the above. – Mike Rockétt Aug 05 '13 at 05:30
  • Thank you for the ideas. I have successfully implemented the filters method which seems to make the most sense! – Kevin Jung Aug 05 '13 at 07:42
  • 3
    Thank you for including the controller example. Want to keep routes "skinny" because they get messy fast in non-trivial applications. – Andrew Koper Jan 12 '14 at 00:32