xetreon / jsonresponse
A simple, lightweight Laravel package for standardized JSON API responses, validation, and logging.
Installs: 124
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/xetreon/jsonresponse
Requires
- php: ^8.1
- illuminate/http: *
- illuminate/support: *
Requires (Dev)
- orchestra/testbench: ^8.0
- phpunit/phpunit: ^10.0
README
A lightweight Laravel package for standardized JSON API responses, validation, and error logging.
Perfect for building clean, consistent REST APIs without repeating boilerplate code.
β Support Me
If you find this package helpful, consider supporting my work:
π Features
- β
Consistent JSON responses (
success,error,validationError) - β Built-in validation helpers with structured error output
- β
Automatic error logging with unique
trace_idfor easier debugging - β Extensible BaseController and BaseException
- β Publishable traits and base classes for full customization
- β Configuration publishing for easy overrides
π¦ Installation
Require the package via Composer:
composer require xetreon/jsonresponse
Publish the config file to customize defaults:
php artisan vendor:publish --tag=config
π§© Usage Example
Hereβs how you can use Xetreon\JsonResponse\Controllers\BaseController in your controllers.
use Xetreon\JsonResponse\Controllers\BaseController; use Illuminate\Http\Request; use App\Models\User; class UserController extends BaseController { public function index(Request $request) { $users = User::query(); // fetchFromQuery automatically applies: // - status filtering (status[]) // - sorting (sort_by & sort_order) // - pagination (per_page) return $this->fetchFromQuery($users, $request->all()); } public function show($id) { return $this->fetchById(User::query(), $id, 'User not found'); } }
π₯ Sample Request JSON
{
"status": ["active", "pending"], // Filter results by status (uses whereIn)
"sort_by": "created_at", // Sort field
"sort_order": "DESC", // Sorting direction (ASC or DESC)
"per_page": 15 // Pagination size
}
π₯ Example cURL Request
curl --location 'https://your-app.test/api/users' \ --header 'Accept: application/json' \ --header 'Content-Type: application/json' \ --data '{ "status": ["active", "pending"], "sort_by": "created_at", "sort_order": "DESC", "per_page": 15 }'
β‘ Direct Usage of Response Methods
You can use the built-in response helper methods in your controllers for quick and consistent JSON responses.
β Success Response
return $this->success(true, ['foo' => 'bar'], 'Operation successful');
return $this->error(false, [], 'Something went wrong', 500);
π‘οΈ Validation Helper
Use the built-in validate() method for consistent request validation and error responses.
Example Usage
$result = $this->validate($request->all(), [ 'email' => 'required|email', 'password' => 'required|min:8', ]); if (!$result['result']) { return $this->validationError($result); }
π Publishing Base Classes and Traits
You can publish the BaseController and BaseException into your application for customization.
π Publish Classes
php artisan vendor:publish --tag=classes
π Publishing Traits
php artisan vendor:publish --tag=xetreon-jsonresponse-traits
π Using the LoggerTrait
The LoggerTrait provides an easy way to log info, warning, and error messages with consistent formatting and automatic file/line code references.
1οΈβ£ Importing & Using the Trait
use Xetreon\JsonResponse\Traits\LoggerTrait; class OrderService { use LoggerTrait; public function processOrder($order) { // Log an informational message $this->createLog('Starting order processing', ['order_id' => $order->id]); try { // Your order processing logic... } catch (\Throwable $e) { // Log an error with context $this->createErrorLog('Order processing failed', [ 'order_id' => $order->id, 'exception' => $e->getMessage() ]); } } }
2οΈβ£ Available Methods
| Method | Level | Parameters | Example |
|---|---|---|---|
createLog() |
Info | string $message, array $context = [], int $trace = 1 |
$this->createLog('User created', ['id' => 1]); |
createWarningLog() |
Warning | string $message, array $context = [], int $trace = 1 |
$this->createWarningLog('User quota nearing limit'); |
createErrorLog() |
Error | string $message, array $context = [], int $trace = 1 |
$this->createErrorLog('Critical failure', ['error' => $e->getMessage()]); |
Parameter Details:
$messageβ A short, human-readable message.$contextβ Additional data to log (automatically JSON-encoded).$traceβ (Optional) Stack trace depth β1for current method,2for caller, etc.
3οΈβ£ Output Example (Log File)
When logging using LoggerTrait, you get standardized log entries:
[2025-09-14 15:30:12] local.INFO: APP_COMMON - Starting order processing {"order_id":123}
[2025-09-14 15:30:13] local.ERROR: APP_COMMON - Order processing failed {"order_id":123,"exception":"Insufficient stock"}
4οΈβ£ Customizing the Log Channel
You can configure which channel LoggerTrait writes to by editing config/xetreon-jsonresponse.php:
'log_channel' => env('XETREON_JSON_LOG_CHANNEL', config('logging.default')),
Or overwrite via .env
XETREON_JSON_LOG_CHANNEL=stack
5οΈβ£ Using Class Codes
You can map specific files to short class codes for more meaningful log identifiers.
Example Configuration (config/xetreon-jsonresponse.php)
'classcode' => [ 'app/Services/OrderService.php' => 'ORD', 'app/Http/Controllers/UserController.php' => 'USR', ],
β Example Log Output
[2025-09-14 15:30:12] local.INFO: ORD:123 - Starting order processing {"order_id":123}
Here you will get the controller code : line number in the log.
β Why Use LoggerTrait?
- Consistent: Ensures the same log format across the entire application.
- Traceable: Automatically adds file name and line number to every log entry.
- Configurable: Lets you choose the log channel per environment.
- Structured: Always logs with JSON-encoded context for easy parsing and analysis.
β οΈ Exception Usage
You can throw a BaseException anywhere in your application code to return a standardized JSON error response.
π» Example
use Xetreon\JsonResponse\Exceptions\BaseException; throw new BaseException('Something went wrong', 500, ['context' => 'extra-data']);
π€ Example JSON Response
{
"result": false,
"data": {
"context": "extra-data"
},
"message": "Something went wrong",
"status_code": 500,
"error": {
"type": "BaseException",
"file": "app/Services/OrderService.php",
"line": 42,
"time": "2025-09-14 16:00:00",
"code": "APP_COMMON"
}
}
β Highlights
file&lineβ Useful in local/dev environments (hidden in production for security).codeβ Class code or module identifier for quick filtering in logs.timeβ Exact timestamp when the exception was thrown.
β οΈ Extending BaseException
You can create custom exceptions that extend BaseException for different error scenarios.
π» Example
<?php namespace App\Exceptions; use Xetreon\JsonResponse\Exceptions\BaseException; class AuthException extends BaseException { public function __construct(string $message = 'Unable to Authenticate User', int $code = 401) { parent::__construct($message, $code); } }
π€ Example Usage
use App\Exceptions\AuthException; throw new AuthException();
β οΈ Adding Extra Data
You can pass extra data to the exception for better debugging and context.
π» Example
throw new AuthException('Token is invalid', 401, [ 'token' => $request->bearerToken(), 'ip' => $request->ip() ]);
4οΈβ£ Class Codes (Optional)
You can define file-specific codes in config/xetreon-jsonresponse.php to make error codes more meaningful:
'classcode' => [ 'app/Exceptions/AuthException.php' => 'AUTH', 'app/Services/OrderService.php' => 'ORD' ],
Now your error JSON will include:
"code": "AUTH:23"
Where 23 is the line number where the exception occurred.
β Benefits
- π₯ Consistent error format β no more HTML errors in API responses.
- π§ Developer-friendly β automatically adds
trace_id,file,line, andtime. - π Extensible β easily create your own custom exception classes.
- π Debuggable β logs all exceptions automatically for post-mortem analysis.