Do the views need to be different, or just the data passed to those views? Seems to me it would just be the data passed to the view that varies
Show different views based on user role
I'm new to Laravel and I'm using v5.1 in my project. I have created most of the business logic and it works great (I'm starting to really love Laravel!), but I could really need some guidance from a experienced Artisan to re-architecture it. I appreciate all the guidance and tips I can get!
Right now I have 4 tables, one for 'users', one for 'personal articles', one for 'club articles' and one for 'clubs'. It has 3 different roles, 'user', 'coach' and 'admin.
'users' can create their own articles and edit their personal info. 'Coaches' can create 'club articles’ that everyone who belongs to the same club can see, they can also create ’users’ but only within their own club. Admins have full permissions.
I have 4 controllers, one AdminsController, CoachesController, ArticlesController and ClubArticlesController where all of them have their own model. Each of these have their own route::resource and views right now using their own permalink, however this current structure is not useful for me.
I would like to have one route for the front-end where articles are going to be displayed for the user, and one view for the back-end. Depending on what role a user has, different sections of the back-end should be shown.
What would be a good practice to structure the routes/controllers to display different admin views depending on roles (I have already the roles defined)? Should I have something like '/frontend’ and ’/backend’ with route::resource, and add conditional statements to the controllers public functions? Or maybe have several routes with conditional statements to it? What would be a good way to accomplish this?
@bobeta You can do that in your view, for example:
// within in some view
@if(Auth::user->hasRole('admin'))
// show content related to it
@endif
And for that hasRole method, you can create it within your User model:
class User
{
/**
* Check if this user belongs to a role
*
* @return bool
*/
public function hasRole($role)
{
return $this->role == $role;
}
}
Please or to participate in this conversation.