Download the PHP package quonain/smart-response without Composer
On this page you can find all versions of the php package quonain/smart-response. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download quonain/smart-response
More information about quonain/smart-response
Files in quonain/smart-response
Package smart-response
Short Description PHP response library for standardizing REST/API responses across JSON, XML, legacy contracts, and Laravel web responses.
License MIT
Homepage https://github.com/quonainejaz-official/smart-response
Informations about the package smart-response
SmartResponse
SmartResponse is a production-ready Laravel package that returns API JSON or Blade / Inertia web views from the same controller method — with automatic request-type detection.
Table of contents
- Features
- Requirements
- Installation
- Quick start
- How API vs Web is detected
- Usage
- Unified response
- HTTP shortcuts
- Facade & helpers
- Response macros
- Pagination
- API meta enrichment
- Rate limiting
- Caching
- Exception handling
- Configuration
- Middleware & events
- Testing
- Changelog & license
Features
| Category | Capabilities |
|---|---|
| Detection | Accept header, expectsJson(), /api/* routes, Bearer tokens (Sanctum / Passport) |
| API | Standard JSON envelope, optional XML, validation errors, exception handler |
| Web | Blade views, redirects, session flash, optional toast |
| Pagination | Length-aware, simple, cursor paginators + API Resources |
| DX | Trait, Facade, global helpers, response()->smart* macros |
| HTTP shortcuts | created, noContent, notFound, unauthorized, forbidden |
| Meta | Auto timestamp, request_id, optional api_version on API responses |
| Extras | Inertia.js, Livewire, i18n, caching, logging, events, OpenAPI examples |
| Framework | Laravel 10 · 11 · 12 · 13 · PHP 8.2+ |
Requirements
- PHP
^8.2 - Laravel
^10.0·^11.0·^12.0·^13.0
Installation
Or pin the latest 1.x release:
Laravel auto-discovers the service provider — no manual registration.
Publish assets
Packagist: After a new release, open your package page and click Update if Composer does not see the latest tag yet. Enable the GitHub hook under package settings for automatic sync.
Quick start
1. Add the trait
| Request type | Result |
|---|---|
API (Accept: application/json, /api/*, Bearer token, …) |
Standard JSON |
Web (text/html, normal browser) |
Blade view users.index with $data, $message, … |
2. Default JSON shape
How API vs Web is detected
SmartResponse treats a request as API when any of these match (configurable in config/smart-response.php):
Acceptcontainsapplication/jsonorapplication/vnd.api+jsonAcceptcontainsapplication/xmlortext/xml- Route matches
api/*or configured prefixes (apiby default) - Laravel
expectsJson()is true (AJAX, etc.) Authorization: Bearer …is present (detection.bearer_as_api— ideal for Sanctum / Passport SPA or mobile apps)
Otherwise the request is handled as Web (view or redirect).
Usage
Unified smartResponse()
HTTP shortcuts
| Trait method | Facade / Manager | Status | Use case |
|---|---|---|---|
smartSuccess() |
SmartResponse::success() |
200 | OK with data |
smartCreated() |
SmartResponse::created() |
201 | Resource created |
smartNoContent() |
SmartResponse::noContent() |
204 | Delete / empty OK |
smartError() |
SmartResponse::error() |
4xx/5xx | Generic error |
smartNotFound() |
SmartResponse::notFound() |
404 | Missing resource |
smartUnauthorized() |
SmartResponse::unauthorized() |
401 | Not logged in |
smartForbidden() |
SmartResponse::forbidden() |
403 | No permission |
smartValidationError() |
SmartResponse::validationError() |
422 | Form / API validation |
Facade & global helpers
Response macros
Pagination
Length-aware — pass a paginator; meta keys are merged automatically:
Cursor — works with cursorPaginate():
Meta includes: per_page, path, next_cursor, prev_cursor, has_more.
API Resources & XML
API meta enrichment
Enabled by default (meta.enabled in config). Every API response can include:
| Meta key | Source |
|---|---|
timestamp |
Current time (ISO 8601) |
request_id |
X-Request-Id header or auto UUID |
api_version |
X-API-Version header or meta.api_version config |
Rate limiting
Returns a standard error JSON with Retry-After header:
Caching (API)
Enable in config, then cache GET API responses:
Without cacheKey, a hash of the full URL + Accept header is used.
Web redirect with flash & toast
Exception handling (API)
Register in bootstrap/app.php (Laravel 11+):
API requests receive the same JSON envelope; web requests fall through to Laravel’s default handling.
Configuration
Publish config/smart-response.php and adjust:
| Key | Description |
|---|---|
api.* |
JSON keys: success, message, data, meta, errors |
detection.* |
JSON/XML accepts, route prefixes, bearer_as_api |
meta.* |
Timestamp, request ID, API version injection |
default_format |
json or xml |
status_codes.* |
Defaults for 200, 201, 204, 401, 403, 404, 422, 429, 500 |
web.* |
Flash / toast session keys, default redirect route |
inertia.enabled |
Inertia.js adapter |
livewire.enabled |
Livewire hooks |
locale.enabled |
Translate message keys via lang files |
cache.enabled |
Cache GET API responses |
logging.enabled |
Log each response |
events.enabled |
SmartResponsePreparing / SmartResponsePrepared |
rate_limit.* |
429 message and retry_after_seconds |
graphql.enabled |
GraphQL response Accept detection |
Multi-language messages
Built-in keys include: users.fetched, users.created, error.not_found, error.unauthorized, error.forbidden, error.rate_limit, and more.
Inertia.js
Middleware
Alias: smart.response (enabled by default)
Events
OpenAPI / Swagger
Testing
Package structure
Changelog
See CHANGELOG.md for version history (1.1.0 — HTTP shortcuts, meta enrichment, cursor pagination, Bearer detection).
Contributing
See CONTRIBUTING.md.
License
MIT © Quonain Ejaz. See LICENSE.