2
0

fix route param matching by using integer offset from router

This commit is contained in:
Sky Johnson 2025-09-11 08:54:10 -05:00
parent 568bd621f9
commit 5cc24441d0
5 changed files with 23 additions and 17 deletions

View File

@ -156,11 +156,11 @@ class Context
}
/**
* param returns a route parameter
* param returns a route parameter by integer index
*/
public function param(string $name, mixed $default = null): mixed
public function param(int $index, mixed $default = null): mixed
{
return $this->request->param($name, $default);
return $this->request->param($index, $default);
}
/**

View File

@ -92,14 +92,21 @@ $result = $router->lookup('GET', '/posts/123');
$name = $context->input('name', 'default'); // POST data only
$search = $context->query('search', ''); // Query params only
$data = $context->jsonValue('data', []); // JSON body only
$id = $context->param('id', 0); // Route params only
$id = $context->param(0, 0); // First route parameter
// Examples with conflicting parameter names
// Examples with route parameters by index
// Route: /users/:userId/posts/:postId
// URL: /users/123/posts/456
$userId = $context->param(0); // "123" (first parameter)
$postId = $context->param(1); // "456" (second parameter)
// Examples with conflicting parameter names in different sources
// URL: /users/123?name=query_name
// POST: name=post_name
// JSON: {"name": "json_name"}
$routeId = $context->param('id'); // "123" from route
$routeId = $context->param(0); // "123" from route (first param)
$queryName = $context->query('name'); // "query_name" from URL
$postName = $context->input('name'); // "post_name" from form
$jsonName = $context->jsonValue('name'); // "json_name" from JSON
@ -215,7 +222,7 @@ $request->params; // route parameters (set by router)
$value = $request->input('key', 'default'); // POST data only
$value = $request->query('key', 'default'); // Query params only
$value = $request->jsonValue('key', 'default'); // JSON body only
$value = $request->param('key', 'default'); // Route params only
$value = $request->param(0, 'default'); // Route params by index
$all = $request->all(); // all input merged
$subset = $request->only(['key1', 'key2']);
$subset = $request->except(['password']);
@ -497,6 +504,8 @@ $app->get('/hello', function(Context $context) {
// Handler with parameters
$app->get('/users/:id/posts/:slug', function(Context $context, $userId, $slug) {
// Parameters are passed as function arguments in order
// Or access via index: $userId = $context->param(0), $slug = $context->param(1)
return [
'user_id' => $userId,
'post_slug' => $slug,

View File

@ -41,7 +41,7 @@ High-performance tree-based router with dynamic parameter support.
### Context
Central request/response wrapper with helper methods to reduce chaining.
- Request helpers: `input()` (POST), `query()` (GET), `jsonValue()` (JSON), `param()` (route), `all()`
- Request helpers: `input()` (POST), `query()` (GET), `jsonValue()` (JSON), `param()` (route by index), `all()`
- Response helpers: `json()`, `text()`, `html()`, `redirect()`, `error()`
- Request info: `ip()`, `userAgent()`, `header()`, `cookie()`, `expectsJson()`
- State management: `set()`, `get()`
@ -49,7 +49,7 @@ Central request/response wrapper with helper methods to reduce chaining.
### Request
Comprehensive HTTP request handling.
- Explicit input methods: `input()` (POST), `query()` (GET), `jsonValue()` (JSON), `param()` (route)
- Explicit input methods: `input()` (POST), `query()` (GET), `jsonValue()` (JSON), `param()` (route by index)
- Header and cookie access
- Content type detection
- URL and security helpers
@ -115,7 +115,7 @@ run(): void
input(string $name, mixed $default = null): mixed // POST data
query(string $name, mixed $default = null): mixed // Query params
jsonValue(string $name, mixed $default = null): mixed // JSON body
param(string $name, mixed $default = null): mixed // Route params
param(int $index, mixed $default = null): mixed // Route params by index
all(): array
only(array $keys): array
except(array $keys): array

View File

@ -94,11 +94,11 @@ class Request
}
/**
* param returns a route parameter
* param returns a route parameter by integer index
*/
public function param(string $name, mixed $default = null): mixed
public function param(int $index, mixed $default = null): mixed
{
return $this->params[$name] ?? $default;
return $this->params[$index] ?? $default;
}
/**

View File

@ -137,10 +137,7 @@ class Web
return;
}
$this->context->request->params = array_combine(
array_map(fn($i) => $i, array_keys($result['params'])),
$result['params']
);
$this->context->request->params = $result['params'];
$handler = $result['handler'];
$response = $handler($this->context, ...$result['params']);