Laravel 4: two different browse pages for one auth based URI

I recently started developing with Laravel 4, and I had a question about routes.

For '/', I would like to have two different browse pages based on the state of the auth user.

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

To achieve this, I played with the filters "auth" and "guest", but I had 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 with the point where, as a registered user, I can display the correct answer, but after logging out I do not see the correct answer as a regular user.

Perhaps this is what needs to be processed in the controller? If someone could point me in the right direction, that would be very helpful.

+4
source share
1 answer

One (simple) option is to use the Auth::check() function to find out if they are logged in:

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

If you wish, you can use the same logic in the controller.

EDIT - Using Filters

If you want to do this in a filter, you can use something like this:

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

and then define a second route (or action in the controller) to handle ordinary users. Although that would mean a different page URL, which I don't think is what you want.

FULL DIRECTION CONTROLLER: (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'); } } } 
+14
source

Source: https://habr.com/ru/post/1495230/


All Articles