Published on September 05 Aug 25
Standardize Your Laravel Responses with YSM Responsable

Perfect β based on the detailed README you provided, here's a comprehensive and engaging blog post to promote YSM/Responsable
, ideal for platforms like dev.to, Medium, Hashnode, or your personal Laravel blog.
β
Standardize Your Laravel Responses with YSM/Responsable
If you've ever found yourself repeating the same response()->json()
or redirect()->with()
boilerplate over and over again, you're not alone.
Whether you're building a RESTful API or a web application, response formatting often gets messy, inconsistent, and scattered across your codebase.
Thatβs exactly why I built YSM/Responsable
β a tiny but powerful Laravel package to standardize JSON and web redirect responses, and bring back the joy of clean, predictable code.
π What Does YSM/Responsable
Do?
It gives you:
β
success()
anderror()
macros for both JSON responses and web redirectsβ
apiSuccess()
andapiError()
helpers for clean API controller syntaxβ
responsable()
andresponsable_forget()
helpers to manage session-based flash dataβ Built-in support for Laravel pagination (including
CursorPaginator
)β Works in Laravel 6+ and PHP 7.2+
π Installation
composer require ysm/responsable
Thatβs it. No need to publish anything β it auto-registers itself in Laravel.
π§ͺ JSON API Responses (Success & Error)
Letβs say you're building a basic posts API:
β Success Response with Collection
public function index()
{
$posts = Post::limit(5)->get();
return Response::success('Posts fetched successfully', PostResource::collection($posts));
}
β Paginated Success Response
public function paginated()
{
$posts = Post::paginate(10);
return Response::success('Posts fetched successfully', PostResource::collection($posts), 200, $posts);
}
Youβll get structured JSON responses like:
{
"status": true,
"message": "Posts fetched successfully",
"code": 200,
"data": [...],
"meta": {
"total": 100,
"per_page": 10,
"current_page": 1,
"last_page": 10
}
}
β Error Response
public function show(Request $request)
{
if (!$request->has('id')) {
return Response::error('Post ID is required');
}
// ...
}
Output:
{
"status": false,
"message": "Post ID is required",
"code": 422,
"errors": []
}
π§Ό Cleaner Syntax with apiSuccess()
/ apiError()
You can also use the global helpers:
return apiSuccess('Post created', ['id' => $post->id]);
return apiError('Something went wrong');
π Web Redirect Responses
YSM/Responsable
also improves redirect responses by attaching structured session data for flash messaging.
β Success Redirect
return redirect()->route('posts.index')->success('Post created successfully', 201, ['id' => $post->id]);
β Error Redirect
return redirect()->back()->error('Failed to update post', 422, ['title' => 'Invalid']);
π― Access Session Data with responsable()
In your controller or Blade view:
$response = responsable();
$response->get('type'); // 'success' or 'error'
$response->get('message'); // The message
$response->get('data'); // Any attached data
$response->get('errors'); // Any attached errors
π Example in a Blade View
@php $r = responsable(); @endphp
@if ($r['type'])
<div class="alert alert-{{ $r['type'] }}">
<strong>{{ $r['message'] }}</strong>
@if ($r['errors'])
<ul>
@foreach ($r['errors'] as $field => $msg)
<li>{{ $field }}: {{ $msg }}</li>
@endforeach
</ul>
@endif
</div>
@endif
π§Ή Clear Session Flash with responsable_forget()
Want to manually clear the stored session flash?
responsable_forget();
Useful if you want full control over the lifetime of flash messages.
π§ IDE Support (Optional)
To enable auto-complete for the macros and helpers:
composer require --dev barryvdh/laravel-ide-helper
php artisan ide-helper:generate
Now you'll get full suggestions for response()->success()
, redirect()->error()
, etc.
π Macro Signatures (Cheat Sheet)
// JSON success
Response::success(string $message = '', array|object $data = [], int $code = 200, ?Paginator $paginator = null);
// JSON error
Response::error(string $message = '', int $code = 422, array $errors = []);
// Web redirect success
redirect()->success(string $message = '', int $code = 200, array|object $data = [], bool $persist = false);
// Web redirect error
redirect()->error(string $message = '', int $code = 422, array $errors = [], bool $persist = false);
β‘ When to Use YSM/Responsable
?
Use this package when you:
Want consistent JSON responses across your API
Need structured flash data for web redirects
Hate rewriting the same response format again and again
Need a testable, predictable way to manage success/error states
π Get Started
π¦ GitHub: https://github.com/DevYSM/responsable
π License: MIT
π
Laravel 6+ | PHP 7.2+
β€οΈ Final Thoughts
This package was born out of frustration with Laravelβs scattered response conventions β one pattern for JSON, another for web redirects, and yet another for session data.
With YSM/Responsable
You can unify your response layer and keep your codebase clean, DRY, and consistent.
Give it a β if it helps, and PRs are always welcome!