Courses

How to Structure Laravel 11 Projects

Structure of Admin/User Role Areas

Summary of this lesson:
- Exploring different approaches for role-based areas
- Using single model with role/permission tables
- Implementing permission checks in controllers
- Managing access through policies

In this lesson, let's talk about structuring projects by areas based on roles. It's a typical scenario for many projects: for example, you have administrator users and regular users.

There could be a more complicated scenario: for instance, in school management software, you might have students, teachers, admins, etc. So, how do we structure such projects?

We have two sub-questions I've seen developers asking:

  1. Do we define separate Models: app/Models/Admin.php, app/Models/User.php, etc?
  2. Do we define separate namespaces: app/Http/Controllers/User, app/Http/Controllers/Admin, etc? Same with Routes?

The answer to the first question is no. Please, please don't create separate models and DB tables for different user roles. It will bite you in the long run: you will need to repeat the same logic in multiple Models, then. Use one model User and then roles/permissions DB tables for this: below, you will see a few examples.

The second question can be answered with "it depends on a project" or "it's your personal preference".

Some projects have totally separate admin areas: different designs, different features, separate login areas, etc. Then, of course, it makes sense to separate the Controllers/routes/views into subfolders.

Other projects are just about checking the permission of the users to access the same pages/functionality. In those cases, I would go for the same folder for Controllers/routes/views and check the permissions as needed.

Let's look at examples of this approach from open-source projects.


Example 1. Check Roles/Permissions in Controller.

This approach to structuring admin/user areas is about using roles/permissions and not changing anything about the structure of the folders.

In other words, you have the same Controllers, common for...

The full lesson is only for Premium Members.
Want to access all 11 lessons of this course? (56 min read)

You also get:

  • 69 courses (majority in latest Laravel 11)
  • Premium tutorials
  • Access to repositories
  • Private Discord