Skip to main content

Requests

The Ecommerce\Http\Request class provides an object-oriented way to interact with the current HTTP request entering your application, as well as retrieve input, cookies, and files that were submitted with the request. It wraps the WordPress REST API request and adds powerful validation and sanitization capabilities.

Retrieving Input​

Retrieve input values from the request with type safety and sanitization.

Get Value​

Retrieve a value from the request attributes.

Description:

The get() method accesses a value by its key. It allows you to specify a default value and an optional datatype or sanitization rule.

Method Signature:

public function get(string $key, mixed $default = null, string|null $type = null): mixed

Parameters:

  • $key - The input key to retrieve.
  • $default - Default value if key is missing.
  • $type - Optional type casting or sanitization rule (e.g., 'int', 'string', 'email').

Examples:

$name = $request->get('name', 'Guest');
$age = $request->get('age', 18, 'int');

Get Integer​

Retrieve an integer value.

Description:

The get_int() method retrieves a value and casts it to an integer.

Method Signature:

public function get_int(string $key, ?int $default = null): ?int

Examples:

$productId = $request->get_int('product_id');
$quantity = $request->get_int('quantity', 1);

Get String / Text​

Retrieve a string value sanitized as text.

Description:

The get_string() or get_text() method retrieves a value and sanitizes it using sanitize_text_field.

Method Signature:

public function get_string(string $key, ?string $default = null): ?string

Examples:

$title = $request->get_string('title');
$description = $request->get_text('description');

Get Boolean​

Retrieve a boolean value.

Description:

The get_bool() method retrieves a value and casts it to a boolean.

Method Signature:

public function get_bool(string $key, bool $default = false): bool

Examples:

$isActive = $request->get_bool('is_active');
$subscribe = $request->get_bool('newsletter', true);

Get Email​

Retrieve a sanitized email address.

Description:

The get_email() method retrieves a value and sanitizes it using sanitize_email.

Method Signature:

public function get_email(string $key, ?string $default = null): ?string

Examples:

$email = $request->get_email('contact_email');

Get HTML Content​

Retrieve HTML content sanitized for safety.

Description:

The get_html() method retrieves a value and sanitizes it using sanitize_textarea_field, preserving simple HTML but stripping dangerous tags.

Method Signature:

public function get_html(string $key, ?string $default = null): ?string

Examples:

$body = $request->get_html('content');

Get All Input​

Retrieve all input data.

Description:

The all() method returns all attributes from the request as an array.

Method Signature:

public function all(): array

Examples:

$input = $request->all();

Validation​

Validate incoming request data.

Validate Data​

Validate the request against a set of rules.

Description:

The validate() method runs validation rules against the request data. If validation fails, it throws a ValidationException. If successful, it returns the validated data.

Method Signature:

public function validate(array $rules): ?array

Parameters:

  • $rules - Array of validation rules (e.g., ['email' => 'required|email']).

Examples:

$validated = $request->validate([
'title' => 'required|string|max:255',
'price' => 'required|numeric|min:0',
]);

Helper Accessors​

Additional helper methods for common data types.

Description:

The Request class includes several other type-specific accessors:

  • get_float($key, $default)
  • get_money($key, $default) - Converts minor units to float.
  • get_date($key, $default)
  • get_datetime($key, $default)
  • get_url($key, $default)
  • get_array($key, $default)
  • get_file_name($key, $default)

Examples:

$price = $request->get_float('price');
$date = $request->get_date('start_date');
$tags = $request->get_array('tags');