initphp/input
Composer 安装命令:
composer require initphp/input
包简介
Read and validate GET, POST and JSON body input with configurable source priority for PHP.
README 文档
README
Read a single request value from the query string, the submitted form fields or the JSON request body — with configurable source priority and optional validation, behind one small API.
Features
- Three input sources behind one API:
get($_GET),post($_POST) andraw(the decoded JSONphp://inputbody). - Twelve priority helpers (
getPost,postRawGet, …) that read the sources in a defined order — the first source that contains the key wins. - Per-call validation powered by initphp/validation: a value that fails its rules yields the default.
- A safe JSON body reader: a scalar or malformed payload becomes an empty set instead of a fatal error.
- A static facade for ergonomic access, plus a fully injectable instance for testing and dependency injection.
- No shared static state between instances; PHPStan level max clean.
Requirements
- PHP 8.1 or later
- initphp/parameterbag
^2.0 - initphp/validation
^2.0 ext-json
Installation
composer require initphp/input
Quick start
With the facade
require_once 'vendor/autoload.php'; use InitPHP\Input\Facade\Inputs as Input; // GET /?name=Jane // echo isset($_GET['name']) ? $_GET['name'] : 'John'; echo Input::get('name', 'John'); // 'Jane'
With an instance
use InitPHP\Input\Inputs; $input = new Inputs(); // reads $_GET, $_POST and php://input $name = $input->get('name', 'John');
You can also hand the sources in explicitly — handy in tests or when the data does not come from the superglobals:
$input = new Inputs( get: ['name' => 'Jane'], post: ['email' => 'jane@example.com'], raw: ['token' => 'abc123'], );
Reading from a single source
$input->get('name', 'guest'); // from $_GET $input->post('email'); // from $_POST $input->raw('token'); // from the JSON request body
Each accessor returns the default (second argument, null when omitted)
if the key is absent. Keys are matched case-sensitively, just like
real HTTP query and body parameters.
Source priority
The priority helpers walk their sources in the order their name reads, left to right, and return the value of the first source that contains the key:
// GET /?year=1999 (no POST, no body) $input->getPost('year', 2015); // 1999 — taken from $_GET // POST year=1999 (no GET) $input->getPost('year', 2015); // 1999 — fell through to $_POST
The full set: getPost, getRaw, getPostRaw, getRawPost, postGet,
postRaw, postGetRaw, postRawGet, rawGet, rawPost, rawGetPost,
rawPostGet.
Validation
Pass a list of validation rules as the third argument. When the resolved value fails, the default is returned:
// if year is present and within 1970–2070 use it, otherwise 2015 $year = $input->getPost('year', 2015, ['range(1970...2070)']); // required + must equal the password_retype field $password = $input->post('password', null, ['required', 'again(password_retype)']);
The first source that owns the key is the one validated; a present but invalid value returns the default and does not fall through to the next source.
Presence checks
$input->hasGet('name'); // isset($_GET['name']) — case-sensitive $input->hasPost('email'); $input->hasRaw('token');
Documentation
Full developer documentation lives in docs/:
getting started, each source, the priority model, validation, the
facade, a complete API reference and an FAQ.
Testing
composer test # PHPUnit composer stan # PHPStan (level max) composer cs-check # PHP-CS-Fixer (dry-run) composer ci # all of the above
Credits
License
Released under the MIT License.
initphp/input 适用场景与选型建议
initphp/input 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 123 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 03 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「validation」 「post」 「request」 「input」 「get」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 initphp/input 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 initphp/input 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 initphp/input 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Symfony bundle that integrates the PostNord PHP SDK
Edit Posts button always visible
Adds request-parameter validation to the SLIM 3.x PHP framework
Extends basic Wordpress features.
A jQuery augmented PHP library for creating and validating HTML forms
A Laravel validator for delimiter-separated list of emails.
统计信息
- 总下载量: 123
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-19