datakrama / lapires
Lapires - Laravel API Response, generalizing all response from your API server
Installs: 3 017
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^7.2|^8.0
- illuminate/support: ^7.0|^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- orchestra/testbench: ^5.0|^6.0
- phpro/grumphp: ^1.1
- phpunit/phpunit: ^8.0|^9.0
README
Lapires is simple Laravel package for generalizing all default response to API-friendly response. With this package, you will have a properly formatted JSON response. However, the response format is fixed, you can not change the format.
Requirements
Laravel Compatibility
Installation
$ composer require datakrama/lapires:"^2.0"
Config (Optional)
If you need to disable custom exception response from this package, publish the config file, and change exception
option to false
.
$ php artisan vendor:publish --provider="Datakrama\Lapires\LapiresServiceProvider"
Usages
All responses will be formatted as follows:
// Success response
{
"success": true,
"message": ...,
"data": {
...
}
}
// Error response
{
"success": false,
"message": ...,
"errors": {
...
}
}
General
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Datakrama\Lapires\Traits\ApiResponser;
class HomeController extends Controller
{
use ApiResponser;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
return view('home');
}
/**
* Success response
*
* @return void
*/
public function success()
{
$data = ['foo' => 'bar'];
$message = 'This is example success response.';
$code = 200;
return $this->successResponse($data, $message, $code);
}
/**
* Error response
*
* @return void
*/
public function error()
{
$code = 500;
$message = 'This is example error response.';
$errors = ['foo' => 'bar'];
return $this->successResponse($code, $message, $errors);
}
}
Thanks
This package is made because inspiration from https://github.com/Cerwyn/laravel-generalizing-response.
Licence
The MIT License (MIT). Please see License File for more information.