Tip
Full Package Description: Helpful advice for doing things better or more easily.
You can install the package via composer:
composer require pepperfm/api-responder-for-laraveluse Pepperfm\ApiBaseResponder\Contracts\ResponseContract;
Then any options you like:
public function __construct(public ResponseContract $json)
{
}
public function index(Request $request)
{
$users = User::query()->whereIn('id', $request->input('ids'))->get();
return $this->json->response($users);
}for pagination
/*
* Generate response.data.meta.pagination from first argument of paginated() method
*/
public function index(Request $request)
{
$users = User::query()->whereIn('id', $request->input('ids'))->paginate();
return $this->json->paginated($users);
}with some data mapping
public function index(Request $request)
{
$users = User::query()->whereIn('id', $request->input('ids'))->paginate();
$dtoCollection = $users->getCollection()->mapInto(UserDto::class);
return $this->json->paginated($dtoCollection->toArray(), $users);
}or
public function index(Request $request)
{
$users = User::query()->whereIn('id', $request->input('ids'))->paginate();
$dtoCollection = $users->getCollection()->mapInto(UserDto::class);
return $this->json->paginated($dtoCollection->toArray(), $users);
}
public function index(Request $request, ResponseContract $json)
{
return $json->response($users);
}
public function index(Request $request)
{
return resolve(ResponseContract::class)->response($users);
}return \ApiBaseResponder::response($users);
return BaseResponse::response($users);The helper-function paginate accepts one argument of LengthAwarePaginator type in backend and returns object of format:
export interface IPaginatedResponse<T> {
current_page: number
per_page: number
last_page: number
data: T[]
from: number
to: number
total: number
prev_page_url?: any
next_page_url: string
links: IPaginatedResponseLinks[]
}
export interface IPaginatedResponseLinks {
url?: any
label: string
active: boolean
}The package recognizes which method it was used from, and, according to REST, if you return one record from the show or edit methods, then the response will be in the
response.data.entity
You can customize methods that should return this format in config file, as well any data-key you like
It's also possible to set any response data-key to any method you need, just add attribute under controller's method ResponseDataKey to set response.data.entity key, or pass any string as parameter:
#[ResponseDataKey]
public function attributeWithoutParam(): JsonResponse
{
return $this->json->response($this->user); // response.data.entity
}
#[ResponseDataKey('random_key')]
public function attributeWithParam(): JsonResponse
{
return $this->json->response($this->user); // response.data.random_key
}composer testPlease see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email Damon3453@yandex.ru instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
This package was generated using the Laravel Package Boilerplate.
