diff --git a/Context.php b/Context.php index a1634ad..2291d98 100644 --- a/Context.php +++ b/Context.php @@ -119,12 +119,7 @@ class Context */ public function validateRequest(array $rules, array $messages = []): Validator { - $data = array_merge( - $this->request->query, - $this->request->body, - $this->request->params - ); - + $data = $this->request->all(); return $this->validate($data, $rules, $messages); } @@ -137,7 +132,7 @@ class Context } /** - * input returns input value from any source (route, query, post, json) + * input returns a form input value (POST data) */ public function input(string $name, mixed $default = null): mixed { @@ -145,11 +140,27 @@ class Context } /** - * param returns a parameter from route params, query params, or post data + * query returns a query parameter value */ - public function param(string $name): mixed + public function query(string $name, mixed $default = null): mixed { - return $this->request->param($name); + return $this->request->query($name, $default); + } + + /** + * jsonValue returns a value from JSON body + */ + public function jsonValue(string $name, mixed $default = null): mixed + { + return $this->request->jsonValue($name, $default); + } + + /** + * param returns a route parameter + */ + public function param(string $name, mixed $default = null): mixed + { + return $this->request->param($name, $default); } /** diff --git a/EXAMPLES.md b/EXAMPLES.md index 52b1644..43e9825 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -88,17 +88,28 @@ $result = $router->lookup('GET', '/posts/123'); ### Request Helpers ```php -// Get input from any source (route params, query, POST, JSON) -$name = $context->input('name', 'default'); -$email = $context->input('email'); +// Get input from specific sources - explicit and predictable +$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 -// Get specific parameter types -$id = $context->param('id'); // route/query/post params -$all = $context->all(); // all input data +// Examples with conflicting parameter names +// URL: /users/123?name=query_name +// POST: name=post_name +// JSON: {"name": "json_name"} + +$routeId = $context->param('id'); // "123" from route +$queryName = $context->query('name'); // "query_name" from URL +$postName = $context->input('name'); // "post_name" from form +$jsonName = $context->jsonValue('name'); // "json_name" from JSON + +// Get all input data merged from all sources (route params override all) +$all = $context->all(); $data = $context->only(['name', 'email']); // specific keys only $data = $context->except(['password']); // all except specified keys -// Check if input exists +// Check if input exists (checks all sources) if ($context->has('email')) { // handle email } @@ -201,9 +212,11 @@ $request->postData; // parsed POST data $request->params; // route parameters (set by router) // Input methods -$value = $request->input('key', 'default'); // from any source -$value = $request->param('key'); // route/query/post only -$all = $request->all(); // all input merged +$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 +$all = $request->all(); // all input merged $subset = $request->only(['key1', 'key2']); $subset = $request->except(['password']); $exists = $request->has('key'); diff --git a/README.md b/README.md index 7783e6b..358d35e 100644 --- a/README.md +++ b/README.md @@ -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()`, `param()`, `all()`, `only()`, `except()`, `has()` +- Request helpers: `input()` (POST), `query()` (GET), `jsonValue()` (JSON), `param()` (route), `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. -- Input parsing from query, POST, JSON, and route parameters +- Explicit input methods: `input()` (POST), `query()` (GET), `jsonValue()` (JSON), `param()` (route) - Header and cookie access - Content type detection - URL and security helpers @@ -112,8 +112,10 @@ run(): void ### Context Class ```php // Request helpers -input(string $name, mixed $default = null): mixed -param(string $name): mixed +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 all(): array only(array $keys): array except(array $keys): array diff --git a/Request.php b/Request.php index e1b8dcb..2b4fbbf 100644 --- a/Request.php +++ b/Request.php @@ -94,42 +94,44 @@ class Request } /** - * param returns a parameter from route params, query params, or post data + * param returns a route parameter */ - public function param(string $name): mixed + public function param(string $name, mixed $default = null): mixed { - return $this->params[$name] ?? $this->queryParams[$name] ?? $this->postData[$name] ?? null; + return $this->params[$name] ?? $default; } /** - * input returns input value from any source (route, query, post, json) + * input returns a form input value (POST data) */ public function input(string $name, mixed $default = null): mixed { - // Check route params first - if (isset($this->params[$name])) { - return $this->params[$name]; + return $this->postData[$name] ?? $default; + } + + /** + * query returns a query parameter value + */ + public function query(string $name, mixed $default = null): mixed + { + return $this->queryParams[$name] ?? $default; + } + + /** + * jsonValue returns a value from JSON body + */ + public function jsonValue(string $name, mixed $default = null): mixed + { + if ($this->contentType() !== 'application/json') { + return $default; } - // Check query params - if (isset($this->queryParams[$name])) { - return $this->queryParams[$name]; + $json = $this->json(); + if (!is_array($json)) { + return $default; } - // Check post data - if (isset($this->postData[$name])) { - return $this->postData[$name]; - } - - // Check JSON body - if ($this->contentType() === 'application/json') { - $json = $this->json(); - if (is_array($json) && isset($json[$name])) { - return $json[$name]; - } - } - - return $default; + return $json[$name] ?? $default; } /**