-
-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Welcome to the uxmansarwar/response PHP package by Uxman Sarwar a Sr. PHP Developer.
This package provides a structured approach to handling API responses in PHP applications. It simplifies response management by offering dynamic result storage, error handling, and user input retrieval in JSON and array formats.
- Author: Uxman Sarwar
- GitHub: [github.com/uxmansarwar](https://github.com/uxmansarwar)
- LinkedIn: [linkedin.com/in/uxmansarwar](https://www.linkedin.com/in/uxmansarwar)
- Email: [[email protected]](mailto:[email protected])
- Efficient response management with a singleton-based approach.
- Dynamic storage for results and errors.
- Automatic capturing of
$_GET
,$_POST
, and JSON payloads. - Supports JSON, array, and custom key-value output formats.
- Compatible with PHP 7.2 and above.
Install via Composer:
composer require uxmansarwar/response
use UxmanSarwar\Response;
Response::init();
Response::result('Data processed successfully');
Response::error('Something went wrong');
echo Response::json();
Output:
{
"result": [
"Data processed successfully"
],
"error": [
"Something went wrong"
]
}
$responseArray = Response::collection();
Response::input(true);
echo Response::json();
Output:
{
"result": [],
"error": [],
"input": {
"key1": "value1",
"key2": "value2"
}
}
Response::key('user')->result('User created successfully');
Response::key('user')->error('Email already exists');
Output:
{
"result": {
"user": [
"User created successfully"
]
},
"error": {
"user": [
"Email already exists"
]
}
}
The package automatically merges $_GET
, $_POST
, and raw JSON input from php://input
. You can retrieve all user inputs by enabling input return:
Response::input(true);
The Response
class implements the Singleton pattern, ensuring a single instance is used throughout your application:
$instance = Response::init();
Retrieve response data as an associative array:
$response = Response::collection();
This package supports automated testing with PHPStan, PHP-CS-Fixer, and PestPHP.
composer install --dev
vendor/bin/phpstan analyse
vendor/bin/pest
vendor/bin/php-cs-fixer fix
- Fork the repository
- Create your feature branch (
git checkout -b feature/YourFeature
) - Commit your changes (
git commit -m 'feat: Add new feature'
) - Push to the branch (
git push origin feature/YourFeature
) - Create a new Pull Request
This project is open-source and available under the [MIT License](https://github.com/uxmansarwar/response/blob/master/LICENSE).
- Email: [[email protected]](mailto:[email protected])
- GitHub: [Uxman Sarwar](https://github.com/uxmansarwar)
- LinkedIn: [Uxman Sarwar](https://www.linkedin.com/in/uxmansarwar)
Thank you for using the Response package! 🚀