@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?
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)
@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.