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

Lars_M's avatar

Dynamic Route booting in middleware

Hi Community,

is it possible to register routes in a middleware? Got a strange behavior: routes where registered/listed. But getting a 404 when visiting the route

BR Lars

0 likes
3 replies
LaryAI's avatar
Level 58

Registering routes within middleware is not a typical practice in Laravel, and it's generally not recommended because the routing should be defined before the middleware layer kicks in. However, if you absolutely need to define routes dynamically, you should do so before the middleware layer is executed, such as in a service provider.

If you're trying to achieve dynamic routing based on some condition, you might want to consider using route model binding or defining your routes in a way that they can handle dynamic parameters.

However, if you still want to experiment with this approach, you could try to use a middleware to modify the request and then handle it in a controller that can interpret the modified request and act accordingly. Here's a conceptual example:

  1. Create a middleware that checks for certain conditions and adds attributes to the request.
namespace App\Http\Middleware;

use Closure;

class DynamicRouteMiddleware
{
    public function handle($request, Closure $next)
    {
        // Check for some condition
        if ($someCondition) {
            // Add attributes to the request that can be used to determine the route
            $request->attributes->add(['dynamic_route' => 'some_value']);
        }

        return $next($request);
    }
}
  1. Register the middleware in your app/Http/Kernel.php.
protected $routeMiddleware = [
    // ...
    'dynamic.route' => \App\Http\Middleware\DynamicRouteMiddleware::class,
];
  1. Use the attribute in a controller to handle the dynamic route.
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DynamicRouteController extends Controller
{
    public function handleDynamicRoute(Request $request)
    {
        $dynamicRoute = $request->attributes->get('dynamic_route');

        // Handle the dynamic route
        // You can switch based on the dynamic_route value or perform some other logic

        return response()->json(['message' => 'This is a dynamic route handling for ' . $dynamicRoute]);
    }
}
  1. Define a fallback route that catches all requests and points them to the controller that can handle dynamic routes.
Route::fallback([DynamicRouteController::class, 'handleDynamicRoute'])->middleware('dynamic.route');

Remember, this is not a standard way to handle routing in Laravel, and it should be used with caution. It's better to define all your routes in the routes files and use route parameters, route model binding, or other Laravel features to handle dynamic routing in a more conventional way.

Snapey's avatar

I can't get past why? There are usually ways to do exactly what you need in Laravel without any weird stuff

martinbean's avatar

is it possible to register routes in a middleware?

@lars_m Sure. But like @snapey says: why? As it seems a bit weird to register routes after a request is received by the Laravel application.

Please or to participate in this conversation.