Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Smart Response

Smart Response Laravel Package

quonain/smart-response

SmartResponse is a Laravel package that returns JSON API responses or Blade/Inertia views from the same controller method, auto-detecting request type (Accept header, /api routes, bearer tokens). Includes pagination, response shortcuts, macros, caching, and meta enrichment.

View on GitHub
Deep Wiki
Context7

SmartResponse is a production-ready Laravel package that lets you return API JSON or Blade web views from the same controller method — automatically detecting the request type. --- ## Features - Automatic API vs Web detection (Accept, expectsJson(), route prefixes) - Standardized JSON API envelope - Blade views with compact.

Frequently asked questions about Smart Response
How does SmartResponse automatically detect if a request is for an API or web view?
SmartResponse uses multiple detection methods: the `Accept` header, Laravel’s `expectsJson()` method, `/api/*` route prefixes, and Bearer token presence (for Sanctum/Passport). You can also customize detection logic via middleware or configuration.
Can I use SmartResponse with Laravel Inertia.js?
Yes, SmartResponse fully supports Inertia.js. It automatically returns Blade views for web requests while still handling JSON responses for API calls, making it ideal for hybrid SPAs.
Does SmartResponse work with Laravel’s built-in API resources (e.g., `ApiResource`)?
Absolutely. SmartResponse integrates seamlessly with Laravel’s API resources, including pagination (length-aware, simple, and cursor-based). It preserves all resource formatting while adding unified meta data.
What Laravel versions does SmartResponse support?
SmartResponse supports Laravel 10, 11, 12, and 13, with PHP 8.2+ required. It’s designed to work with modern Laravel conventions and avoids breaking changes.
How do I handle errors or exceptions uniformly across API and web responses?
SmartResponse includes built-in exception handling for API responses (e.g., validation errors, 4xx/5xx) and can be extended for web views. You can customize error formats via macros or middleware.
Will SmartResponse slow down my API responses?
The package adds minimal overhead for detection and response formatting. For high-traffic APIs, leverage caching (e.g., `Cache::remember`) or disable meta enrichment if unnecessary.
Can I use SmartResponse with packages like `spatie/laravel-api` or `apiato/apiato`?
Potential conflicts may arise if those packages enforce strict API detection (e.g., middleware). SmartResponse prioritizes Laravel’s native methods, but you can override detection logic or use middleware to resolve conflicts.
How do I test controllers using SmartResponse in PHPUnit?
Mock the `SmartResponse` facade or use its testing utilities to isolate controller logic. For example, test API responses with `expectsJson()` and web responses with `expectsView()` assertions.
Does SmartResponse support XML or GraphQL responses?
By default, it focuses on JSON and Blade views. For XML, you’d need to extend the package via macros or middleware. GraphQL isn’t natively supported but could be integrated via custom response logic.
How do I migrate an existing Laravel project to SmartResponse incrementally?
Start with non-critical controllers (e.g., admin panels) and replace `return response()->json()` or `return view()` with `return SmartResponse::make()`. Use feature flags or middleware to toggle SmartResponse per route.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky