uxmansarwar / response
A lightweight PHP response handler for structured API responses.
Fund package maintenance!
uxmansarwar
Buy Me A Coffee
Requires
- php: >=7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.35
- mockery/mockery: ^1.6
- pestphp/pest: ^2.34
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.4
This package is auto-updated.
Last update: 2025-04-12 00:09:55 UTC
README
Overview
Response
is a lightweight PHP library designed to streamline API response handling. It follows a singleton-based pattern to store and retrieve results, errors, and user input efficiently. The package is PSR-4 compliant and fully compatible with PHP 7.2+ and PHP 8.2.
Key Features
- ✅ Singleton pattern to prevent redundant object creation.
- ✅ Structured response handling for JSON and array outputs.
- ✅ Collect API results & errors dynamically.
- ✅ Retrieve user input from
$_GET
,$_POST
, and JSON payloads. - ✅ Flexible data retrieval (JSON, array, collection format).
- ✅ Works with Laravel, Symfony, CodeIgniter, WordPress, and Core PHP.
Installation
Install via Composer
You can install this package using Composer:
composer require uxmansarwar/response
Manual Installation
Alternatively, clone this repository and include it in your project:
git clone https://github.com/uxmansarwar/Response.git
Then, include the autoloader:
require 'vendor/autoload.php';
Usage Examples
1. Initializing the Response Handler
use UxmanSarwar\Response; Response::init();
2. Storing Results
Response::result("User created successfully"); Response::result(["id" => 1, "name" => "John Doe"]);
3. Handling Errors
Response::error("Invalid API request"); Response::error("Failed to connect to the database");
4. Retrieving Response Data
JSON Output:
echo Response::json();
Example Output:
{ "result": [ "User created successfully", { "id": 1, "name": "John Doe" } ], "error": [ "Invalid API request", "Failed to connect to the database" ] }
Array Output:
$response_array = Response::collection(); print_r($response_array);
Advanced Features
5. Grouping Responses with a Key
Response::key("user")->result(["id" => 2, "name" => "Jane Doe"]); echo Response::json();
6. Include User Input in Response
Response::input(true); echo Response::json();
If a request is made with:
GET /api.php?id=10&name=Alice
The output will include:
"input": { "id": "10", "name": "Alice" }
7. Custom Error & Result Keys
You can define custom keys for results and errors:
Response::key("dns");
Use Case Example: API Response Handling
Response::init(); if ($_SERVER['REQUEST_METHOD'] !== 'POST') { Response::error("Only POST requests are allowed"); } else { $data = Response::$_INPUT; if (!isset($data['username']) || empty($data['username'])) { Response::error("Username is required"); } else { Response::result("User registered successfully"); } } echo Response::json();
Why Use This Package?
✅ For Laravel & Symfony Developers: Use it as a service-based response handler.
✅ For WordPress Developers: Improve structured AJAX responses.
✅ For REST API Development: Optimize API response handling with minimal effort.
✅ For Microservices: Centralize error handling and response formatting.
Testing the Package
To install and run tests:
composer install vendor/bin/phpstan analyse src --level=max vendor/bin/pest