Clean URL Multi-Role Dashboard Laravel with Breeze

Posted on

When building a multi-role application in Laravel, you may want to redirect users to their respective dashboards after logging in. Instead of having multiple routes like /admin/dashboard or /user/dashboard, you can simplify things by using a single route (/) to dynamically load the correct dashboard based on the user’s role. This article will walk you through the steps to implement Clean URL Multi-Role Dashboard Laravel 11 with Laravel Breeze for authentication.

The Goal

We want to:

  1. Use / as the root URL for the dashboard.
  2. Dynamically load the appropriate view (admin.dashboard, user.dashboard, etc.) based on the user’s role.
  3. Ensure that only authenticated users can access the dashboard.

Steps to Create the Multi-Role Dashboard

1. Define the Single Root Route

Laravel makes it easy to handle routing with conditional logic. The idea is to have a single route for /, and based on the user’s role, we’ll return the correct dashboard view.

Here’s how to define the root route:

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    $role = auth()->user()->role ?? null;

    switch ($role) {
        case 'admin':
            return view('admin.dashboard');
        case 'user':
            return view('user.dashboard');
        case 'superadmin':
            return view('superadmin.dashboard');
        default:
            abort(403, 'Unauthorized');
    }
})->middleware('auth')->name('dashboard');

Explanation:

  1. Route / is protected by the auth middleware, ensuring only logged-in users can access it.
  2. The user’s role is checked using auth()->user()->role.
  3. The dashboard view is returned based on the user’s role (admin.dashboard, user.dashboard, or superadmin.dashboard).
  4. If the role is invalid or not recognized, a 403 Unauthorized error is triggered.

2. Redirect Users After Login Using Laravel Breeze

In Laravel 11 with Breeze, after a successful login, users are automatically redirected to /dashboard by default. To modify this so users are redirected to / (the root route), we need to customize the redirect behavior after login.

Since Breeze uses Fortify for authentication, you can modify the HomeController or simply adjust the redirect path in the AuthenticatedSessionController.

Update the Redirect Path in AuthenticatedSessionController
  1. Open app/Http/Controllers/Auth/AuthenticatedSessionController.php.
  2. Modify the store method to redirect users to / after login based on their role:
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AuthenticatedSessionController extends Controller
{
    public function store(LoginRequest $request): RedirectResponse
    {
        $request->authenticate();

        $request->session()->regenerate();

        return redirect()->intended('/');
    }
}

With this setup, after logging in, users will always be redirected to /, where the dashboard will be loaded based on their role.

3. Create the Dashboard Views

You need to have Blade files for each user role’s dashboard. Create these views in the resources/views directory:

  • resources/views/admin/dashboard.blade.php
  • resources/views/user/dashboard.blade.php
  • resources/views/superadmin/dashboard.blade.php

Each view can contain role-specific information and functionality.

Advantages of This Approach

  1. Cleaner URLs: The dashboard is accessible via a single URL (/), no need for sub-paths like /admin/dashboard or /user/dashboard.
  2. Simpler Role Management: By using a single route for the dashboard, adding or modifying roles is easy and straightforward.
  3. Security: The auth middleware ensures that only authenticated users can access the dashboard. Unauthorized users will see a 403 Unauthorized error.

Full Example

Below is the full implementation of the route and controller setup:

Route in web.php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    $role = auth()->user()->role ?? null;

    switch ($role) {
        case 'admin':
            return view('admin.dashboard');
        case 'user':
            return view('user.dashboard');
        case 'superadmin':
            return view('superadmin.dashboard');
        default:
            abort(403, 'Unauthorized');
    }
})->middleware('auth')->name('dashboard');

Controller in AuthenticatedSessionController.php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AuthenticatedSessionController extends Controller
{
    public function store(LoginRequest $request): RedirectResponse
    {
        $request->authenticate();

        $request->session()->regenerate();

        return redirect()->intended('/');
    }
}

With this setup, Laravel 11 and Breeze handle user authentication and redirection cleanly. By using a single route for all dashboards, your application is more streamlined, and users will be directed to their correct dashboard based on their role.

This approach:

  • Keeps URLs clean and simple.
  • Makes it easy to add new roles and dashboards in the future.
  • Ensures secure role-based access to different dashboards.

Now, your Laravel application is ready for role-based redirection to personalized dashboards in a cleaner and more efficient manner! Happy coding!