fastpress/request
Composer 安装命令:
composer require fastpress/request
包简介
A powerful and flexible HTTP request handling library for PHP, optimized for use with the Fastpress framework. Provides an easy-to-use interface for accessing request data and handling common request operations.
README 文档
README
This repository contains the Request class, a crucial component of the fastpress/framework. The Request class provides a comprehensive interface for handling HTTP requests in PHP applications. It simplifies accessing request data such as GET, POST, COOKIE, and SERVER variables, and includes methods for common request operations.
Features
- Easy retrieval of GET, POST, COOKIE, and SERVER data.
- Convenience methods for checking the HTTP request method (GET, POST, PUT, DELETE).
- Utilities for common tasks like checking for Ajax requests and secure connections.
- Flexibility to extend or modify for custom use-cases.
Installation
To use this component, first ensure you have fastpress/framework installed. This Request class is a dependency of the framework and is meant to be used within its context.
If you are managing your project with Composer, you can add this dependency directly by running:
composer require fastpress/request
Ensure that this aligns with the version constraints of fastpress/framework.
Usage
To use the Request class, create an instance of it in your PHP application:
use Fastpress\Http\Request; $request = new Request();
Methods
validateCsrf(): bool
Validates the CSRF token.
$isValid = $request->validateCsrf();
- Returns: true if the CSRF token is valid, otherwise throws a RuntimeException.
generateCsrfToken(): string
- Generates a new CSRF token and stores it in the session.
$token = $request->generateCsrfToken();
Returns: The generated CSRF token.
input(string $key, mixed $default = null, bool $sanitize = true): mixed
Retrieves an input value from GET, POST, or JSON data.
$name = $request->input('name'); $age = $request->input('age', 25); $rawInput = $request->input('comment', null, false);
$key: The input key. $default: Optional. The default value to return if the key is not found. $sanitize: Optional. Whether to sanitize the value. Defaults to true. Returns: The input value.
file(string $key): ?array
Retrieves an uploaded file.
$uploadedFile = $request->file('image');
$key: The file key. Returns: An array containing file information, or null if the file is not found.
hasFile(string $key): bool
Checks if an uploaded file exists and was uploaded successfully.
if ($request->hasFile('document')) { // Process the file }
$key: The file key. Returns: true if the file exists and was uploaded successfully, false otherwise.
isJson(): bool
Checks if the request content type is JSON.
if ($request->isJson()) { // Process the JSON data }
Returns: true if the content type is JSON, false otherwise. getMethod(): string Gets the HTTP request method.
$method = $request->getMethod();
Returns: The HTTP request method. getIp(): ?string Gets the client's IP address.
$ipAddress = $request->getIp();
Returns: The client's IP address, or null if it cannot be determined. accepts(string $contentType): bool Checks if the client accepts the given content type.
if ($request->accepts('application/json')) { // Send JSON response }
$contentType: The content type to check. Returns: true if the client accepts the content type, false otherwise. header(string $key, mixed $default = null): mixed Retrieves a header value.
PHP $authToken = $request->header('Authorization');
$key: The header key.
$default: Optional. The default value to return if the header is not found.
Returns: The header value.
get(string $key, mixed $default = null): mixed
Retrieves a GET parameter.
```php
$page = $request->get('page', 1);
$key: The GET parameter key. $default: Optional. The default value to return if the key is not found. Returns: The GET parameter value. post(string $key, mixed $default = null): mixed Retrieves a POST parameter.
$email = $request->post('email');
$key: The POST parameter key. $default: Optional. The default value to return if the key is not found. Returns: The POST parameter value. json(string $key, mixed $default = null): mixed Retrieves a value from the parsed JSON request body.
$userId = $request->json('user_id');
$key: The JSON key. $default: Optional. The default value to return if the key is not found. Returns: The JSON value. getBody(): string Gets the raw request body.
$requestBody = $request->getBody();
Returns: The raw request body. validate(array $rules): array Validates the request data against the given rules.
$rules = [ 'name' => 'required|min:3', 'email' => 'required|email', ]; $errors = $request->validate($rules); if (!empty($errors)) { // Handle validation errors }
$rules: An array of validation rules. Returns: An array of validation errors. setUrlParams(array $params): void Sets the URL parameters.
$request->setUrlParams(['id' => 10, 'slug' => 'my-article']);
$params: An array of URL parameters. param(string $key, mixed $default = null): mixed Retrieves a URL parameter.
$articleId = $request->param('id');
$key: The URL parameter key. $default: Optional. The default value to return if the key is not found. Returns: The URL parameter value. all(): array Gets all input data (GET, POST, JSON).
$allInputData = $request->all();
Returns: An array containing all input data.
fastpress/request 适用场景与选型建议
fastpress/request 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 76 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 02 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「php」 「http」 「request」 「http request」 「fastpress」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 fastpress/request 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 fastpress/request 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 fastpress/request 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
repository php library
http客户端
Easily add Excepct-CT header to your project
Build mini web servers in PHP for testing
Alfabank REST API integration
统计信息
- 总下载量: 76
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 11
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-02-13