Skip to content

Middleware not executed for dynamic routes inside group (/{id:\d+}) but works for static routes #316

@bhushanzade

Description

@bhushanzade

Describe the bug

When defining a dynamic route (/{id:\d+}) inside a route group that uses middleware, the middleware is not executed.

Static routes inside the same middleware group work correctly.

For example:

  • /user/profile → middleware executes correctly
  • /practices/1/submit (static route) → middleware executes correctly
  • /practices/{id:\d+}/submit (dynamic route) → middleware does not execute

The request goes directly to the controller method without passing through the middleware.


To Reproduce

  1. Define a middleware (example: auth) that logs or validates authentication.
  2. Create a route group with middleware:
$app->group('/', [
  'middleware' => 'auth',
  function () use ($app) {

    $app->get('/user/profile', function () {
        return response()->json(['message' => 'profile']);
    });

    $app->post('/practices/{id:\d+}/submit', function () {
        return response()->json(['message' => 'submitted']);
    });

  }
]);
  1. Register middleware:
$app->registerMiddleware('auth', function () {
    error_log("Auth middleware called");
});
  1. Send request:
POST /practices/1/submit
  1. Observe that middleware is not executed.

  2. Replace dynamic route with static:

$app->post('/practices/1/submit', ...);
  1. Middleware executes correctly.

Expected behavior

Middleware should execute for dynamic routes inside a group the same way it does for static routes.

The dynamic route /practices/{id:\d+}/submit should trigger the auth middleware before reaching the controller.


Additional context

  • Leaf version: "leafs/leaf": "^4.4"
  • PHP version: PHP 7.4.30
  • Server environment: XAMP

This issue only occurs with dynamic parameters inside grouped routes using middleware. Static routes inside the same group work as expected.


Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions