I have a Laravel 4 application with a resource Poll
// routes.php
Route::resource('polls', 'PollController');
I do not want anyone to be able to list all the polls, except if the user is authenticated and if (s)he is an admin. This was my solution:
// PollController.php
public function index() {
if (Auth::check() && Auth::user()->admin) {
return View::make('polls.index', Poll::all());
}
return View::make('polls.create', []);
}
This code works just fine, but it is not very clean code. For once, I make this the "admin check" in a few places. Also it does not feel like it follows the practice of "A function should be doing just 1 thing".
I was wondering if there is a cleaner way to handle cases where the response changes depending on whether the user is logged in and is an admin?