tbn/json-annotation-bundle
Symfony bundle that adds a @Json annotation for controllers to automatically return JSON responses. Wraps successful return arrays and exceptions into a consistent payload (success/data/message), with configurable keys, HTTP codes, and optional POST query echo/auth errors.
success, data, message) across controllers.exception_code, data_key), aligning with modular design principles.Controller base class may complicate integration with modern frameworks (e.g., API Platform, Symfony UX).JsonResponse).App\Exceptions\Handler.JsonPreHookEvent), but E2E tests may require stubbing bundle logic.post_query_back).data_key) may misalign with frontend expectations.json.pre_hook listeners could fail silently if not properly subscribed.Response::json() + middleware (e.g., App\Middleware\ApiResponse).JsonResponse + ApiPlatform for APIs.phpunit/phpunit@^9.5 + symfony/http-foundation@^6.0).nelmio/api-doc-bundle (for OpenAPI) + custom middleware.fruitcake/laravel-cors + Illuminate\Http\JsonResponse.@Json() annotations be gradually replaced with native alternatives?AppKernel.laravel/symfony-bundle), but not recommended for Laravel 8+.@Json() to quantify effort."tbn/json-annotation-bundle": "dev-master").AppKernel.php:
new tbn\JsonAnnotationBundle\JsonAnnotationBundle(),
trait JsonResponseTrait {
protected function jsonResponse(array $data, bool $success = true): JsonResponse {
return response()->json([
'success' => $success,
'data' => $data,
]);
}
}
@Json() annotations with native responses or middleware.src/
├── Annotation/Json.php # Attribute-based
├── EventListener/JsonListener.php
└── DependencyInjection/...
json_annotation parameters in config.yml.HttpKernel.public function handle($request, Closure $next) {
try {
$response = $next($request);
if ($response instanceof JsonResponse) {
$response->setData([
'success' => true,
'data' => $response->getData(),
]);
}
return $response;
} catch (\Exception $e) {
return response()->json([
'success' => false,
'message' => $e->getMessage(),
], 500);
}
}
serialization_context instead.@ParamConverter—test thoroughly.@Json() with native responses or middleware.JsonResponse + middleware).config.yml).JsonPreHookEvent issues).CONTRIBUTING.md for future developers.@Json() usage and configuration in the team wiki.json.pre_hook), failures could break API access.blackfire.io).HttpCache or Laravel’s response cache separately.k6 or artillery.| **Failure
How can I help you explore Laravel packages today?