PHP code example of guanguans / laravel-api-response
1. Go to this page and download the library: Download guanguans/laravel-api-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/ */
guanguans / laravel-api-response example snippets
namespace App\Http\Controllers\Api;
use Guanguans\LaravelApiResponse\Contracts\ApiResponseContract;
use Guanguans\LaravelApiResponse\Facades\ApiResponseFacade;
use Guanguans\LaravelApiResponse\Middleware\SetJsonAcceptHeader;
use Guanguans\LaravelApiResponse\Support\Traits\ApiResponseFactory;
use Illuminate\Http\JsonResponse;
class Controller extends \App\Http\Controllers\Controller
{
use ApiResponseFactory;
public function __construct()
{
$this->middleware(SetJsonAcceptHeader::class)->only('exceptionHandler');
}
public function getInstance(): JsonResponse
{
/** @var \Guanguans\LaravelApiResponse\ApiResponse $apiResponse */
// $apiResponse = ApiResponseFacade::getFacadeRoot();
// $apiResponse = resolve(ApiResponseContract::class);
// $apiResponse = app(ApiResponseContract::class);
$apiResponse = $this->apiResponse();
return $apiResponse->success($data);
}
public function exampleMethods(): JsonResponse
{
// return $this->apiResponse()->error($message);
// return $this->apiResponse()->exception($exception);
return $this->apiResponse()->success($data);
}
/**
* @response
*
*
namespace App\Http\Controllers\Api;
use Guanguans\LaravelApiResponse\Support\Traits\ApiResponseFactory;
use Guanguans\LaravelApiResponse\Tests\Laravel\Models\User;
use Illuminate\Http\JsonResponse;
class Controller extends \App\Http\Controllers\Controller
{
use ApiResponseFactory;
public function example(): JsonResponse
{
$user = User::query()->with(['country', 'posts'])->first();
return $this->apiResponse()->success($user);
}
}
namespace App\Http\Controllers\Api;
use Guanguans\LaravelApiResponse\Support\Traits\ApiResponseFactory;
use Guanguans\LaravelApiResponse\Tests\Laravel\Models\User;
use Illuminate\Http\JsonResponse;
class Controller extends \App\Http\Controllers\Controller
{
use ApiResponseFactory;
public function example(): JsonResponse
{
$users = User::query()->with(['country', 'posts'])->get();
return $this->apiResponse()->success($users);
}
}
namespace App\Http\Controllers\Api;
use Guanguans\LaravelApiResponse\Support\Traits\ApiResponseFactory;
use Guanguans\LaravelApiResponse\Tests\Laravel\Models\User;
use Illuminate\Http\JsonResponse;
class Controller extends \App\Http\Controllers\Controller
{
use ApiResponseFactory;
public function example(): JsonResponse
{
$simplePaginate = User::query()->with(['country', 'posts'])->simplePaginate(3);
return $this->apiResponse()->success($simplePaginate);
}
}
namespace App\Http\Controllers\Api;
use Guanguans\LaravelApiResponse\Support\Traits\ApiResponseFactory;
use Guanguans\LaravelApiResponse\Tests\Laravel\Models\User;
use Guanguans\LaravelApiResponse\Tests\Laravel\Resources\UserResource;
use Illuminate\Http\JsonResponse;
class Controller extends \App\Http\Controllers\Controller
{
use ApiResponseFactory;
public function example(): JsonResponse
{
$userResource = UserResource::make(User::query()->with(['country', 'posts'])->first());
return $this->apiResponse()->success($userResource);
}
}
namespace App\Http\Controllers\Api;
use Guanguans\LaravelApiResponse\Support\Traits\ApiResponseFactory;
use Guanguans\LaravelApiResponse\Tests\Laravel\Models\User;
use Guanguans\LaravelApiResponse\Tests\Laravel\Resources\UserCollection;
use Illuminate\Http\JsonResponse;
class Controller extends \App\Http\Controllers\Controller
{
use ApiResponseFactory;
public function example(): JsonResponse
{
$userCollection = UserCollection::make(User::query()->with(['country', 'posts'])->get());
return $this->apiResponse()->success($userCollection);
}
}
namespace App\Http\Controllers\Api;
use Guanguans\LaravelApiResponse\Support\Traits\ApiResponseFactory;
use Illuminate\Http\JsonResponse;
class Controller extends \App\Http\Controllers\Controller
{
use ApiResponseFactory;
public function example(): JsonResponse
{
return $this->apiResponse()->error('This is an error.');
}
}
namespace App\Http\Controllers\Api;
use Guanguans\LaravelApiResponse\Support\Traits\ApiResponseFactory;
use Illuminate\Http\JsonResponse;
class Controller extends \App\Http\Controllers\Controller
{
use ApiResponseFactory;
public function example(): JsonResponse
{
config()->set('app.debug', false);
$runtimeException = new \RuntimeException('This is a runtime exception.');
return $this->apiResponse()->exception($runtimeException);
}
}
namespace App\Http\Controllers\Api;
use Guanguans\LaravelApiResponse\Support\Traits\ApiResponseFactory;
use Illuminate\Http\JsonResponse;
class Controller extends \App\Http\Controllers\Controller
{
use ApiResponseFactory;
public function example(): JsonResponse
{
config()->set('app.debug', true);
$runtimeException = new \RuntimeException('This is a runtime exception.');
return $this->apiResponse()->exception($runtimeException);
}
}
namespace App\Http\Controllers\Api;
use Guanguans\LaravelApiResponse\Middleware\SetJsonAcceptHeader;
use Illuminate\Http\JsonResponse;
class Controller extends \App\Http\Controllers\Controller
{
public function __construct()
{
$this->middleware(SetJsonAcceptHeader::class)->only('example');
}
public function example(): JsonResponse
{
config()->set('app.debug', false);
throw new \RuntimeException('This is a runtime exception.');
}
}
namespace App\Http\Controllers\Api;
use Guanguans\LaravelApiResponse\Support\Traits\ApiResponseFactory;
use Illuminate\Http\JsonResponse;
class Controller extends \App\Http\Controllers\Controller
{
use ApiResponseFactory;
public function example(): JsonResponse
{
config()->set('app.debug', false);
config()->set('app.locale', 'zh_CN');
$runtimeException = new \RuntimeException('This is a runtime exception.');
return $this->apiResponse()->exception($runtimeException);
}
}
static function (array $data, \Closure $next): JsonResponse {
if ($data['data'] instanceof \iterable) {
$data['data'] = iterator_to_array($data['data']);
}
return $next($data);
};
namespace App\Http\Controllers\Api;
use Guanguans\LaravelApiResponse\Support\Traits\ApiResponseFactory;
use Illuminate\Http\JsonResponse;
class Controller extends \App\Http\Controllers\Controller
{
use ApiResponseFactory;
public function example(): JsonResponse
{
return $this
->apiResponse()
// ->unshiftPipes(...)
->pushPipes(
static function (array $data, \Closure $next): JsonResponse {
if ($data['data'] instanceof \iterable) {
$data['data'] = iterator_to_array($data['data']);
}
return $next($data);
}
)
// ->unshiftExceptionPipes(...)
// ->pushExceptionPipes(...)
->success($iterator);
}
}