简体   繁体   中英

Laravel 5 - pagination of results in header instead of the JSON

I'm dealing now with pagination of search results in my API made in Laravel and I came across this in the documentation:

https://laravel.com/docs/5.3/pagination#converting-results-to-json

It seems nice but at the same time I'm quite concerned about the fact that it alters the structure of the result, since it hides the returned data inside the "data" key.

The problem is, until now I was returning a non-paginated JSON-encoded array of objects and including pagination this way would either require making remarkable modifications in the front-end or it would bring inconsitency across the API (there would be API calls which return the data nested and other ones which return them directly - I don't like it).

I would probably like more to include the pagination metadata in the header of the response which won't mess up my current structure of responses. Do you know about any out-of-the-box solution like this (ideally a Laravel package)? Or what other approach would you recommend me to resolve my dilemma?

There seems to be two options for pagination information in an API - in the response body as Laravel does, and in the header information (like github does ).

I am having this issue myself with Laravel with a new project, and I have pretty much decided that I want to do pagination in headers - it just feels a bit more right (admittedly, this is a hotly debated topic).

I haven't yet built this, so may change my mind as time goes on, but building a piece of middleware and attaching it to the API group seems to be the way to go.

In that middleware, we need to get the full response before you add the headers, something like:

public function handle($request, Closure $next, $guard = null)
{
    // let other middleware handle the request first
    $response = $next($request);

    // get paging links here
    // <url>; rel="next", <url>; rel="prev", <url>; rel="first", <url>; rel="last"
    $response->header("Link: " . $links);

    return $response;
}

The Laravel docs on middleware are recommended reading too.

Tip: Never stick to a mistake or you'll be doomed to hell, believe me.

By the above sentence mean that your frontend is a problem, not the laravel response, I've been using this pagination in many apps and these always worked for me, mean this page_data and page_meta_data are served in a single object because these belongs togather.

I want to show you two examples

Instagram Instagram帖子分页

Airbnb 在此处输入图片说明

I didn't mean to say that these are standards, but still you can think that this is the best approach.

Laravel Package

I'm almost 100% sure that there is no laravel package for you to help.

Hack/Fix for your issue

Laravel gives you freedom of adding headers to a response all along.

return response()->headers($key, $value)->headers($key, $value);

Disclaimer: This is not the right way to do this, rather this is not even a close to the right way.

I have the same issue. I like link header pagination, github uses it, since response body contains only the "raw" data and not meta data.

I think this Laravel middleware example can help you.

Route::get('items', 'ItemController@index')->middleware('link-header-pagination');

https://github.com/y-zono/laravel-link-header-pagination

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM