1. Go to this page and download the library: Download three/laravel-response library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
three / laravel-response example snippets
// app/Exceptions/Handler.php
// 引入以后对于 API 请求产生的异常都会进行格式化数据返回
// 要求请求头 header 中包含 /json 或 +json,如:Accept:application/json
// 或者是 ajax 请求,header 中包含 X-Requested-With:XMLHttpRequest;
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
use Three\LaravelResponse\Support\Traits\ExceptionTrait;
class Handler extends ExceptionHandler
{
use ExceptionTrait;
// ...
}
public function index()
{
$users = User::all();
return Response::success(new UserCollection($users));
}
public function paginate()
{
$users = User::paginate(5);
return Response::success(new UserCollection($users));
}
public function simplePaginate()
{
$users = User::simplePaginate(5);
return Response::success(new UserCollection($users));
}
public function item()
{
$user = User::first();
return Response::success(new UserResource($user));
}
public function array()
{
return Response::success([
'name' => 'Jiannel',
'email' => '[email protected]'
],'', ResponseEnum::SERVICE_REGISTER_SUCCESS);
}