PHPackages                             uxmansarwar/response - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. uxmansarwar/response

ActiveLibrary[API Development](/categories/api)

uxmansarwar/response
====================

Elegant and lightweight PHP Composer Package for standardized API responses with error, ttl, query and result handling. Package developed by UxmanSarwar a Senior PHP Developer since 2013

v4.0.0(1y ago)2166↓93.8%MITPHPPHP &gt;=8.0

Since Feb 22Pushed 1y ago1 watchersCompare

[ Source](https://github.com/uxmansarwar/response)[ Packagist](https://packagist.org/packages/uxmansarwar/response)[ Docs](https://github.com/uxmansarwar/response)[ Fund](https://www.buymeacoffee.com/uxmansarwar)[ GitHub Sponsors](https://github.com/uxmansarwar)[ RSS](/packages/uxmansarwar-response/feed)WikiDiscussions master Synced today

READMEChangelog (8)Dependencies (5)Versions (18)Used By (0)

📦 Response – Elegant PHP API Response Handler
=============================================

[](#-response--elegant-php-api-response-handler)

[![Packagist](https://camo.githubusercontent.com/5ad31cb9442d9bbfb85175e1973fb864b5784f48f5643b78548215ffe79190e5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f75786d616e7361727761722f726573706f6e7365)](https://packagist.org/packages/uxmansarwar/response)[![PHP Version](https://camo.githubusercontent.com/c43a736f738fa982a023ed7fc827895810d2ca0f353aa53674e0691bccea1034/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344382e302d626c7565)](https://www.php.net/)[![License](https://camo.githubusercontent.com/f8df3091bbe1149f398a5369b2c39e896766f9f6efba3477c63e9b4aa940ef14/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e)](LICENSE)[![GitHub Stars](https://camo.githubusercontent.com/179b66f342e9e57b1cbedc7c7b24e40ef0859ea54deb788c2615370485ec5f6d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f75786d616e7361727761722f726573706f6e73653f7374796c653d736f6369616c)](https://github.com/uxmansarwar/response)

🔍 Overview
----------

[](#-overview)

**`uxmansarwar/response`** is a powerful yet lightweight PHP package that makes it easy to manage structured API responses. Built with modern PHP practices, it follows a clean singleton pattern and supports result grouping, TTL, debug queries, and more. Ideal for Laravel, Symfony, WordPress, CodeIgniter, or raw PHP projects.

Developed and maintained by [Uxman Sarwar](https://github.com/uxmansarwar), a senior PHP developer since 2013.

✅ Features
----------

[](#-features)

- Singleton-based fluent API
- Add results, errors, queries, TTL, and input metadata
- Auto-collects `$_GET`, `$_POST`, and raw JSON input
- Customizable result/error key groups with `key()` and `index()`
- Get structured responses as JSON or array
- Great for APIs, microservices, and AJAX handlers

---

⚙️ Installation
---------------

[](#️-installation)

### Via Composer

[](#via-composer)

```
composer require uxmansarwar/response
```

---

🚀 Quick Start
-------------

[](#-quick-start)

### Initialize

[](#initialize)

```
use UxmanSarwar\Response;

Response::init();
```

### Add Result

[](#add-result)

```
Response::result(['id' => 1, 'name' => 'Alice']);
```

### Add Error

[](#add-error)

```
Response::error('Invalid request type');
```

### Grouped Result with Key/Index

[](#grouped-result-with-keyindex)

```
Response::key('user')->index('info')->result(['email' => 'user@example.com']);
```

### Set Time-To-Live (TTL)

[](#set-time-to-live-ttl)

```
Response::ttl(60);
```

### Attach Debug Query

[](#attach-debug-query)

```
Response::query('SELECT * FROM users WHERE id = 1');
```

### Include Input Data

[](#include-input-data)

```
Response::input(true);
```

### Output as JSON

[](#output-as-json)

```
echo Response::json();
```

### Output as Array

[](#output-as-array)

```
print_r(Response::array());
```

---

💡 Example Use Case: API Endpoint
--------------------------------

[](#-example-use-case-api-endpoint)

```
Response::init();

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    Response::error("Only POST requests are allowed");
} else {
    $data = Response::$_INPUT;
    if (empty($data['username'])) {
        Response::error("Username is required");
    } else {
        Response::result("User registered successfully");
    }
}

echo Response::json();
```

---

🧪 Testing
---------

[](#-testing)

```
composer install
vendor/bin/phpstan analyse src --level=max
vendor/bin/pest
```

---

🌐 Use Cases
-----------

[](#-use-cases)

- Laravel: Replace default `response()->json()` with a fluent helper
- WordPress: Handle AJAX with structured output
- Symfony: Wrap controller responses with grouped structure
- REST APIs: Make consistent error/result formatting
- Microservices: Inject debug info and TTL for downstream caching

---

👨‍💻 About the Author
--------------------

[](#‍-about-the-author)

This package is created by [Uxman Sarwar](https://github.com/uxmansarwar), a full-stack PHP Laravel developer.

- GitHub: [@uxmansarwar](https://github.com/uxmansarwar)
- LinkedIn: [Uxman Sarwar](https://linkedin.com/in/uxmansarwar)
- Email:

If you found this package useful, consider ⭐ starring the repo and sharing it with other developers.

---

🔗 SEO &amp; GitHub Keywords
---------------------------

[](#-seo--github-keywords)

PHP API response library, structured API output PHP, response formatter, Laravel response helper, singleton response PHP, uxmansarwar response composer, REST API output PHP, error handler class PHP, api response json PHP, PHP response class Laravel

---

📥 Composer Install Reminder
---------------------------

[](#-composer-install-reminder)

```
composer require uxmansarwar/response

```

Happy coding! 🚀

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance47

Moderate activity, may be stable

Popularity16

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~7 days

Recently: every ~20 days

Total

16

Last Release

398d ago

Major Versions

v1.0.3.x-dev → v2.0.12025-02-22

v2.0.1.x-dev → v3.0.02025-03-11

v3.1.x-dev → v4.0.02025-06-01

PHP version history (3 changes)v1.0.0PHP ^7.0

v1.0.2PHP &gt;=7.0

v4.0.0PHP &gt;=8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/6656d90144ef72f57969a0b32204c68bde80b4c4b7390dadcde28a840d4d6b6b?d=identicon)[uxmansarwar](/maintainers/uxmansarwar)

---

Top Contributors

[![uxmansarwar](https://avatars.githubusercontent.com/u/51332876?v=4)](https://github.com/uxmansarwar "uxmansarwar (22 commits)")

---

Tags

responsephpjsonapierror handlingdata collectorapi outputresponse handlerstructured responseresponse formatter

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/uxmansarwar-response/health.svg)

```
[![Health](https://phpackages.com/badges/uxmansarwar-response/health.svg)](https://phpackages.com/packages/uxmansarwar-response)
```

###  Alternatives

[obiefy/api-response

Simple Laravel package to return Json responses.

17325.0k](/packages/obiefy-api-response)[nilportugues/jsonapi-bundle

Symfony 2 &amp; 3 JSON API Transformer Package

11446.7k](/packages/nilportugues-jsonapi-bundle)[nilportugues/api-problems

PSR7 Response implementation for the Problem Details for HTTP APIs

1749.6k2](/packages/nilportugues-api-problems)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
