Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pengaentu's avatar

Structuring code for different user roles

What would be the best way to structure code for when you have different roles?

For example, let's say I am building a website like Laracasts. I have 3 roles - Admin, Teachers and Students. I have a model 'Course' with can be indexed by all three roles. So admins get all courses, teachers get all courses they created and students get all courses that they subscribed to.

How do I structure code for this? Do I create 3 different controllers and 3 different routes for each role? Or do I create a single api/controller and just modify the query according to the role? If I were to use Laravel permissions by Spatie, how would I make use of it here? Is there any open source project which has similar features that I can learn from?

0 likes
1 reply
pengaentu's avatar

What would be the best way to structure code for when you have different roles?

For example, let's say I am building a website like Laracasts. I have 3 roles - Admin, Teachers and Students. I have a model 'Course' with can be indexed by all three roles. So admins get all courses, teachers get all courses they created and students get all courses that they subscribed to.

How do I structure code for this? Do I create 3 different controllers and 3 different routes for each role? Or do I create a single api/controller and just modify the query according to the role? If I were to use Laravel permissions by Spatie, how would I make use of it here? Is there any open source project which has similar features that I can learn from? https://xrank.one/xvideos/

I got this,,...

Please or to participate in this conversation.