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

mmuqiitf's avatar

Laravel Best Practice for Controllers

Hello i have a question about best practice using controllers in Laravel. In example, i have 3 role in my application like : admin, teacher and students. So i have a folder structure for controllers like this : `

		Admin
					DashboardController
					SubjectController
					.....
		Student
					DashboardController
					SubjectController
					.....
		Teacher
					DashboardController
					SubjectController
					.....

`

Is this the best practice for Laravel or what should i do to make development easier?

0 likes
3 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

One approach to make development easier and more organized is to use a resource-based approach for controllers. This means that instead of having separate controllers for each role, you would have a single controller for each resource (e.g. subjects, users, etc.) and use middleware to restrict access based on the user's role.

Here's an example of how you could structure your controllers using this approach:

app/
├── Http/
│   ├── Controllers/
│   │   ├── Admin/
│   │   │   ├── SubjectController.php
│   │   │   └── UserController.php
│   │   ├── Student/
│   │   │   ├── SubjectController.php
│   │   │   └── UserController.php
│   │   ├── Teacher/
│   │   │   ├── SubjectController.php
│   │   │   └── UserController.php
│   │   ├── Auth/
│   │   │   ├── LoginController.php
│   │   │   └── RegisterController.php
│   │   └── HomeController.php

In this example, we have separate folders for each role, but each folder contains only the controllers for the resources that the role has access to. We also have a separate folder for authentication-related controllers.

To restrict access to certain routes based on the user's role, you can use middleware. For example, you could create an AdminMiddleware that checks if the user is an admin before allowing access to certain routes. Here's an example of how you could use middleware to restrict access to the SubjectController for admins only:

// app/Http/Controllers/Admin/SubjectController.php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;

class SubjectController extends Controller
{
    public function index()
    {
        // Only admins can access this route
    }
}
// app/Http/Middleware/AdminMiddleware.php

namespace App\Http\Middleware;

use Closure;

class AdminMiddleware
{
    public function handle($request, Closure $next)
    {
        if (!auth()->user()->isAdmin()) {
            abort(403, 'Unauthorized action.');
        }

        return $next($request);
    }
}
// app/Http/Kernel.php

protected $routeMiddleware = [
    // ...
    'admin' => \App\Http\Middleware\AdminMiddleware::class,
];
// routes/web.php

Route::middleware('admin')->group(function () {
    Route::get('/admin/subjects', 'Admin\SubjectController@index');
});

This approach allows you to keep your controllers organized and avoid duplication, while still providing fine-grained access control based on the user's role.

1 like
newbie360's avatar

For me i will use, no nested namespace

AdminDashboardController
AdminSubjectController

StudentDashboardController
StudentSubjectController

TeacherDashboardController
TeacherSubjectController
SahilMarkana's avatar

For me I am use role permission for different role so imy strucher as -> FrontendController ->diffrent controller of frontend -> BackendController ->diffrent controller of backend

Please or to participate in this conversation.