Skip to content

Route parameter type coercion #357

@philipobenito

Description

@philipobenito

Automatically coerce route parameters from strings to typed values based on pattern matchers or explicit configuration.

Current behaviour:
All route variables are strings, even when matched by typed patterns like {id:number}.

Proposed behaviour:

$router->map('GET', '/users/{id:number}', function (ServerRequestInterface $request, array $args) {
    // $args['id'] is now an int, not a string
});

Considerations:

  • This is a breaking change to the variable presentation contract
  • Needs careful design around what types are supported (int, float, bool)
  • Should respect the pattern matcher type (:number implies int)
  • Could be opt-in via strategy or router configuration
  • Interacts with the parameter injection feature (Inject route parameters as controller method arguments #356)

Target: 7.x

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions