Why not using separate admin routes and/or controllers? Create a namespace for your admin area.
Route::group(array('prefix' => 'admin', 'namespace' => 'Admin'), function() {
Route::get('preferences', array('as' => 'admin.preferences', 'uses' => 'UserController@yourfunction');
Route::get('preferences', array('as' => 'admin.preferences', 'uses' => 'UserController@yourfunction');
});
I would probably use one method in one controller, but query the user model to see if the user was an admin. If the user is an admin they can edit any user if they aren't they are restricted to their own user prefs.
public function preferences($userId = null)
{
// is this admin
if(\Auth::user()->admin == 1)
{
$user = \User::find($userId);
}else{
$user = \Auth::user();
}
}
Thanks for your replies.
@Edwin-Luijten If I understand correctly your suggestion still ends up with 4 code stuffed methods (2 for regular user, 2 for admin). I'd like to code as DRY as possible, so I'm afraid this won't be a viable solution.
@elite123 You're hardcoding the admin dependency inside a method, which isn't quite future proof (imagine another role 'staff' needs access as well).
I posted this question also on StackOverflow and I'm actually liking the answer posted over there: http://stackoverflow.com/a/22166514
Do you guys have any further remarks on this?
@Propaganistas Ah - you only mentioned an admin, if you need roles look at something like Entrust (https://github.com/Zizaco/entrust) then you can just do something like:
$user->can('edit_all_user_prefs')
If you decide to go the separate controller/view route there are filters available
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community