PHP code example of flugger / laravel-responder
1. Go to this page and download the library: Download flugger/laravel-responder 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/ */
flugger / laravel-responder example snippets
public function index()
{
$resource = new Collection(User::all(), new UserTransformer());
return response()->json((new Manager)->createData($resource)->toArray());
}
public function index()
{
return responder()->success(User::all())->respond();
}
Flugg\Responder\ResponderServiceProvider::class,
'Responder' => Flugg\Responder\Facades\Responder::class,
'Transformation' => Flugg\Responder\Facades\Transformation::class,
$app->register(Flugg\Responder\ResponderServiceProvider::class);
class_alias(Flugg\Responder\Facades\Responder::class, 'Responder');
class_alias(Flugg\Responder\Facades\Transformation::class, 'Transformation');
public function index(Responder $responder)
{
return $responder->success();
}
return $responder->error();
return responder()->success();
return responder()->error();
return Responder::success();
return Responder::error();
return $this->success();
return $this->error();
return responder()->success()->respond();
return responder()->error()->respond();
return responder()->success()->respond(201, ['x-foo' => true]);
return responder()->error()->respond(404, ['x-foo' => false]);
return responder()->success()->toArray();
return responder()->error()->toArray();
'decorators' => [
\Flugg\Responder\Http\Responses\Decorators\StatusCodeDecorator::class,
\Flugg\Responder\Http\Responses\Decorators\SuccessFlagDecorator::class,
],
return responder()->success()->decorator(ExampleDecorator::class)->respond();
return responder()->error()->decorator(ExampleDecorator::class)->respond();
\Flugg\Responder\Http\Responses\Decorators\PrettyPrintDecorator::class,
\Flugg\Responder\Http\Responses\Decorators\EscapeHtmlDecorator::class,
return responder()->success()->respond();
return responder()->success(Product::all())->respond();
return responder()->success(Product::where('id', 1))->respond();
return responder()->success(Product::first()->shipments())->respond();
return responder()->success(Product::all(), function ($product) {
return ['name' => $product->name];
})->respond();
return responder()->success(Product::all(), ProductTransformer::class)->respond();
return responder()->success(Product::all(), new ProductTransformer)->respond();
class Product extends Model implements Transformable {}
/**
* Get a transformer for the class.
*
* @return \Flugg\Responder\Transformers\Transformer|string|callable
*/
public function transformer()
{
return ProductTransformer::class;
}
use Flugg\Responder\Contracts\Transformers\TransformerResolver;
public function boot()
{
$this->app->make(TransformerResolver::class)->bind([
\App\Product::class => \App\Transformers\ProductTransformer::class,
\App\Shipment::class => \App\Transformers\ShipmentTransformer::class,
]);
}
return responder()->success(Product::all())->respond();
public function getResourceKey(): string {
return 'products';
}
return responder()->success(Product::all(), ProductTransformer::class, 'products')->respond();
return responder()->success(Product::paginate())->respond();
$paginator = Product::paginate();
$adapter = new IlluminatePaginatorAdapter($paginator);
return responder()->success($paginator->getCollection())->paginator($adapter)->respond();
if ($request->has('cursor')) {
$products = Product::where('id', '>', request()->cursor)->take(request()->limit)->get();
} else {
$products = Product::take(request()->limit)->get();
}
$cursor = new Cursor(request()->cursor, request()->previous, $products->last()->id ?? null, Product::count());
return responder()->success($products)->cursor($cursor)->respond();
return responder()->success(Product::all())->with('shipments')->respond();
return responder()->success(Product::all())->with('shipments', 'orders.customer')->respond();
return responder()->success(Product::all())->with(['shipments' => function ($query) {
$query->whereNull('shipped_at');
}])->respond();
return responder()->success(Product::all())->without('comments')->respond();
return responder()->success(Product::all())->only('id', 'name')->respond();
return responder()->success(Product::all())->with('shipments')->only([
'products' => ['id', 'name'],
'shipments' => ['id']
])->respond();
return responder()->success(Product::all())->meta(['count' => Product::count()])->respond();
return responder()->success()->serializer(JsonApiSerializer::class)->respond();
return responder()->success()->serializer(new JsonApiSerializer())->respond();
namespace App\Transformers;
use App\User;
use Flugg\Responder\Transformers\Transformer;
class ProductTransformer extends Transformer
{
/**
* List of available relations.
*
* @var string[]
*/
protected $relations = [];
/**
* A list of autoloaded default relations.
*
* @var array
*/
protected $load = [];
/**
* Transform the model.
*
* @param \App\Product $product
* @return array
*/
public function transform(Product $product): array
{
return [
'id' => (int) $product->id,
];
}
}
protected $relations = [
'shipments' => ShipmentTransformer::class,
];
protected $relations = [
'shipments' => ShipmentTransformer::class,
'orders' => OrderTransformer::class,
];
protected $load = [
'shipments' => ShipmentTransformer::class,
'orders' => OrderTransformer::class,
];
/**
* Include related shipments.
*
* @param \App\Product $product
* @return mixed
*/
public function
public function roduct->shipments->take($parameters->get('limit'));
}
/**
* Load shipments with constraints.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function loadShipments($query)
{
return $query->whereNull('shipped_at');
}
/**
* Filter \Illuminate\Database\Eloquent\Collection $shipments
* @return \Illuminate\Support\Collection
*/
public function filterShipments($shipments)
{
return $shipments->filter(function ($shipment) {
return is_null($shipment->shipped_at);
});
}
return (new ProductTransformer)->transform(Product::first());
return array_merge((new ProductTransformer)->transform($product = Product::first()), [
'shipments' => $product->shipments->map(function ($shipment) {
return (new ShipmentTransformer)->transform($shipment);
})
]);
public function index(TransformBuilder $transformation)
{
return $transformation->resource(Product::all())->with('shipments')->transform();
}
return $transformation->resource(Product::all())->serializer(NoopSerializer::class)->transform();
public function __construct(Transformation $transformation)
{
$transformation->make(Product::all())->with('shipments')->transform();
}
transformation(Product::all())->with('shipments')->transform();
Transformation::make(Product::all())->with('shipments')->transform();
return responder()->success(Product::all(), function ($product) {
return ['soldOut' => (bool) $product->sold_out];
})->respond();
protected $middleware = [
// ...
\Flugg\Responder\Http\Middleware\ConvertToSnakeCase::class,
];
return responder()->error()->respond();
return responder()->error('sold_out_error')->respond();
return responder()->error('sold_out_error', 'The requested product is sold out.')->respond();
return [
'sold_out_error' => 'The requested product is sold out.',
];
use Flugg\Responder\ErrorMessageResolver;
public function boot()
{
$this->app->make(ErrorMessageResolver::class)->register([
'sold_out_error' => 'The requested product is sold out.',
]);
}
return responder()->error('sold_out')->data(['shipments' => Shipment::all()])->respond();
return responder()->error()->serializer(ExampleErrorSerializer::class)->respond();
return responder()->success()->serializer(new ExampleErrorSerializer())->respond();
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Flugg\Responder\Exceptions\Handler as ExceptionHandler;
public function render($request, Exception $exception)
{
$this->convertDefaultException($exception);
if ($exception instanceof HttpException) {
return $this->renderResponse($exception);
}
return parent::render($request, $exception);
}
if ($request->wantsJson()) {
$this->convertDefaultException($exception);
if ($exception instanceof HttpException) {
return $this->renderResponse($exception);
}
}
/**
* A list of default exception types that should not be converted.
*
* @var array
*/
protected $dontConvert = [
ModelNotFoundException::class,
];
$this->convert($exception, [
InvalidValueException => PageNotFoundException,
]);
$this->convert($exception, [
MaintenanceModeException => function ($exception) {
throw new ServerDownException($exception->retryAfter);
},
]);
namespace App\Exceptions;
use Flugg\Responder\Exceptions\Http\HttpException;
class SoldOutException extends HttpException
{
/**
* The HTTP status code.
*
* @var int
*/
protected $status = 400;
/**
* The error code.
*
* @var string|null
*/
protected $errorCode = 'sold_out_error';
/**
* The error message.
*
* @var string|null
*/
protected $message = 'The requested product is sold out.';
}
/**
* Retrieve additional error data.
*
* @return array|null
*/
public function data()
{
return [
'shipments' => Shipment::all()
];
}
throw new SoldOutException();
shell
php artisan vendor:publish --provider="Flugg\Responder\ResponderServiceProvider"
shell
php artisan make:transformer ProductTransformer
shell
php artisan make:transformer ProductTransformer --model="App\Models\Product"
shell
php artisan make:transformer ProductTransformer --plain