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

whoisthisstud's avatar

Dynamic Route Prefix issue

I had what I thought was a working dynamic route prefix and it almost works perfectly...

When I attempt to access any route outside of the attached route group, while logged in, I get a "Missing required parameter" error. The reason is due to the dashboard route, linked in the navbar, that is shown when the user is logged in.

Nothing changes if I add the middleware to $routeMiddleware versus $middlewareGroups.

What am I missing? Is there a better, or recommended, method of implementing a dynamic prefix?

// web.php

// Any routes outside of below group won't work, if user is logged in
// ...

// The route group with dynamic prefix
Route::group(['prefix' => '{prefix}', 'middleware' => ['prefix','auth']], function () {

    // Dashboard
    Route::get('/dashboard', 'DashboardController@index')->name('dashboard-home');

    // ...

});

// Kernel.php

// ...

protected $middlewareGroups = [
    // ...

    'prefix' => [
        'web',
        \App\Http\Middleware\SetUserRoutePrefix::class,
    ],
];

// ...

// SetUserRoutePrefix.php

<?php

namespace App\Http\Middleware;

use URL;
use Auth;
use Closure;
use Illuminate\Http\Request;

class SetUserRoutePrefix
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        if( $request->user() ) {

            if( ! $request->user()->hasRole(['SuperAdmin','Admin']) ) {

                if( $request->user()->hasRole(['Contractor']) ) {
                    $prefix =  'contractor';
                } else {
                    $prefix =  'user';
                }

            } else {

                $prefix = 'admin';

            }
			
            // Is this the correct way to do this?
            URL::defaults([ 'prefix' => $prefix ]);

        } else {

            return redirect()->route('public.home');
            
        }

        return $next($request);
    }
}

// Error

Illuminate\Routing\Exceptions\UrlGenerationException
Missing required parameter for [Route: dashboard-home] [URI: {prefix}/dashboard] [Missing parameter: prefix]. (View: /my-app/resources/views/_partials/public-navbar.blade.php)
0 likes
7 replies
SilenceBringer's avatar

@whoisthisstud you have error message - missing required param for dashboard-home route. Are you sure you pass the prefix to you route? Can you show how you output this route in your code?

whoisthisstud's avatar

Uhm... isn't the SetUserRoutePrefix::class passing the prefix param?

<a href="{{ route('dashboard-home') }}">Dashboard</a>
whoisthisstud's avatar

Right, I am accessing a route that is defined outside of the prefix middleware grouped routes. The dashboard-home route is within the prefix middleware grouped routes and linked in the navbar shown on the route that is outside of the prefix middleware group. Do you recommend I add the prefix logic to the navbar file, in order to pass that prefix param in the navbar link?

whoisthisstud's avatar

Is there a better or other recommended method to accomplish this?

SilenceBringer's avatar
Level 55

@whoisthisstud hm... here

protected $middlewareGroups = [
    // ...

    'prefix' => [
        'web',
        \App\Http\Middleware\SetUserRoutePrefix::class,
    ],
];

you doing something wrong. try to put your middleware inside web group

protected $middlewareGroups = [
    // ...

    'web' => [
        // other middlewares
        \App\Http\Middleware\SetUserRoutePrefix::class,
    ],
];

and remove prefix from routeMiddleware and from your routes/web.php

Please or to participate in this conversation.