explicit input methods
This commit is contained in:
parent
6184d28dda
commit
568bd621f9
31
Context.php
31
Context.php
@ -119,12 +119,7 @@ class Context
|
|||||||
*/
|
*/
|
||||||
public function validateRequest(array $rules, array $messages = []): Validator
|
public function validateRequest(array $rules, array $messages = []): Validator
|
||||||
{
|
{
|
||||||
$data = array_merge(
|
$data = $this->request->all();
|
||||||
$this->request->query,
|
|
||||||
$this->request->body,
|
|
||||||
$this->request->params
|
|
||||||
);
|
|
||||||
|
|
||||||
return $this->validate($data, $rules, $messages);
|
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
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
31
EXAMPLES.md
31
EXAMPLES.md
@ -88,17 +88,28 @@ $result = $router->lookup('GET', '/posts/123');
|
|||||||
### Request Helpers
|
### Request Helpers
|
||||||
|
|
||||||
```php
|
```php
|
||||||
// Get input from any source (route params, query, POST, JSON)
|
// Get input from specific sources - explicit and predictable
|
||||||
$name = $context->input('name', 'default');
|
$name = $context->input('name', 'default'); // POST data only
|
||||||
$email = $context->input('email');
|
$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
|
// Examples with conflicting parameter names
|
||||||
$id = $context->param('id'); // route/query/post params
|
// URL: /users/123?name=query_name
|
||||||
$all = $context->all(); // all input data
|
// 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->only(['name', 'email']); // specific keys only
|
||||||
$data = $context->except(['password']); // all except specified keys
|
$data = $context->except(['password']); // all except specified keys
|
||||||
|
|
||||||
// Check if input exists
|
// Check if input exists (checks all sources)
|
||||||
if ($context->has('email')) {
|
if ($context->has('email')) {
|
||||||
// handle email
|
// handle email
|
||||||
}
|
}
|
||||||
@ -201,8 +212,10 @@ $request->postData; // parsed POST data
|
|||||||
$request->params; // route parameters (set by router)
|
$request->params; // route parameters (set by router)
|
||||||
|
|
||||||
// Input methods
|
// Input methods
|
||||||
$value = $request->input('key', 'default'); // from any source
|
$value = $request->input('key', 'default'); // POST data only
|
||||||
$value = $request->param('key'); // route/query/post 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
|
$all = $request->all(); // all input merged
|
||||||
$subset = $request->only(['key1', 'key2']);
|
$subset = $request->only(['key1', 'key2']);
|
||||||
$subset = $request->except(['password']);
|
$subset = $request->except(['password']);
|
||||||
|
|||||||
10
README.md
10
README.md
@ -41,7 +41,7 @@ High-performance tree-based router with dynamic parameter support.
|
|||||||
|
|
||||||
### Context
|
### Context
|
||||||
Central request/response wrapper with helper methods to reduce chaining.
|
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()`
|
- Response helpers: `json()`, `text()`, `html()`, `redirect()`, `error()`
|
||||||
- Request info: `ip()`, `userAgent()`, `header()`, `cookie()`, `expectsJson()`
|
- Request info: `ip()`, `userAgent()`, `header()`, `cookie()`, `expectsJson()`
|
||||||
- State management: `set()`, `get()`
|
- State management: `set()`, `get()`
|
||||||
@ -49,7 +49,7 @@ Central request/response wrapper with helper methods to reduce chaining.
|
|||||||
|
|
||||||
### Request
|
### Request
|
||||||
Comprehensive HTTP request handling.
|
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
|
- Header and cookie access
|
||||||
- Content type detection
|
- Content type detection
|
||||||
- URL and security helpers
|
- URL and security helpers
|
||||||
@ -112,8 +112,10 @@ run(): void
|
|||||||
### Context Class
|
### Context Class
|
||||||
```php
|
```php
|
||||||
// Request helpers
|
// Request helpers
|
||||||
input(string $name, mixed $default = null): mixed
|
input(string $name, mixed $default = null): mixed // POST data
|
||||||
param(string $name): mixed
|
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
|
all(): array
|
||||||
only(array $keys): array
|
only(array $keys): array
|
||||||
except(array $keys): array
|
except(array $keys): array
|
||||||
|
|||||||
48
Request.php
48
Request.php
@ -94,44 +94,46 @@ 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
|
public function input(string $name, mixed $default = null): mixed
|
||||||
{
|
{
|
||||||
// Check route params first
|
return $this->postData[$name] ?? $default;
|
||||||
if (isset($this->params[$name])) {
|
|
||||||
return $this->params[$name];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check query params
|
/**
|
||||||
if (isset($this->queryParams[$name])) {
|
* query returns a query parameter value
|
||||||
return $this->queryParams[$name];
|
*/
|
||||||
}
|
public function query(string $name, mixed $default = null): mixed
|
||||||
|
{
|
||||||
// Check post data
|
return $this->queryParams[$name] ?? $default;
|
||||||
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];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* jsonValue returns a value from JSON body
|
||||||
|
*/
|
||||||
|
public function jsonValue(string $name, mixed $default = null): mixed
|
||||||
|
{
|
||||||
|
if ($this->contentType() !== 'application/json') {
|
||||||
return $default;
|
return $default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$json = $this->json();
|
||||||
|
if (!is_array($json)) {
|
||||||
|
return $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $json[$name] ?? $default;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* all returns all input data merged from all sources
|
* all returns all input data merged from all sources
|
||||||
*/
|
*/
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user