Support the ongoing development of Laravel.io →
Forms Architecture
Last updated 1 year ago.
0

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');
});
Last updated 1 year ago.
0

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();
    }
}
Last updated 1 year ago.
0

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?

Last updated 1 year ago.
0

@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

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.