Installation
Add the bundle to your composer.json:
composer require artprima/jsend-bundle
Enable it in config/bundles.php:
return [
// ...
Artprima\JsendBundle\ArtprimaJsendBundle::class => ['all' => true],
];
First Use Case
Inject the JsendResponse service into a controller and use it to wrap API responses:
use Artprima\JsendBundle\Response\JsendResponse;
class ApiController extends AbstractController
{
public function successResponse(JsendResponse $jsend): Response
{
return $jsend->success(['message' => 'Operation completed']);
}
public function errorResponse(JsendBundle $jsend): Response
{
return $jsend->fail('Something went wrong');
}
}
Where to Look First
config/packages/artprima_jsend.yaml (if auto-generated).src/Response/JsendResponse.php for available methods (success, fail, error).src/Twig/JsendExtension.php for template helpers.API Response Standardization
Replace raw JsonResponse with JsendResponse for consistent JSend-compliant payloads:
// Before
return new JsonResponse(['status' => 'success', 'data' => $data]);
// After
return $jsend->success($data);
Error Handling
Use fail() for client errors (e.g., validation) and error() for server errors:
try {
$result = $service->execute();
return $jsend->success($result);
} catch (ValidationException $e) {
return $jsend->fail($e->getMessage());
} catch (\Exception $e) {
return $jsend->error($e->getMessage());
}
Middleware Integration Wrap middleware responses for uniformity:
public function handle(Request $request, Closure $next): Response
{
try {
return $next($request);
} catch (UnauthorizedHttpException $e) {
return $jsend->fail('Unauthorized', 401);
}
}
Twig Integration (if enabled) Use Twig helpers to render JSend-compliant JSON in templates:
{{ dump(jsend_success({'message': 'Hello'})) }}
JsendResponse from message handlers for async workflows.JsonLdSerializer to use JsendResponse for consistent serialization.JsendResponse from event listeners for API consistency.No Auto-Configuration
Unlike popular bundles, this lacks auto-configuration. Manually register the bundle in bundles.php and verify services are loaded.
Deprecated Symfony Versions
The composer.json requires PHP 5.3.3 and Symfony 2.x components (e.g., sensio/framework-extra-bundle). Ensure compatibility with your stack.
Missing Documentation No formal docs exist. Reverse-engineer usage from:
src/Response/JsendResponse.php (core methods).tests/ (if available) for edge cases.Twig Extension Not Auto-Enabled
If using Twig, manually enable the extension in config/packages/twig.yaml:
twig:
extensions:
- Artprima\JsendBundle\Twig\JsendExtension
Service Not Found? Clear cache and verify the bundle is registered:
php bin/console cache:clear
php bin/console debug:container jsend
JSON Output Mismatch
Check if the response is being double-serialized (e.g., by a global listener). Use dd($jsend->getData()) to inspect raw payloads.
Custom Response Types
Extend JsendResponse to add domain-specific statuses:
class CustomJsendResponse extends JsendResponse
{
public function warning($message, $data = []): Response
{
return $this->createResponse('warning', $message, $data);
}
}
Override Serialization
Modify the serialize() method in JsendResponse to customize the JSON structure (e.g., add metadata).
Event Listeners
Attach listeners to kernel.response to post-process JsendResponse objects (e.g., add logging or headers):
public function onKernelResponse(FilterResponseEvent $event): void
{
$response = $event->getResponse();
if ($response instanceof JsendResponse) {
$response->headers->set('X-JSend', 'true');
}
}
artprima_jsend.yaml. Override behavior via DI:
services:
Artprima\JsendBundle\Response\JsendResponse:
arguments:
$defaultStatus: 'success' # Override default status
How can I help you explore Laravel packages today?