gabriel007's avatar

Laravel 5.2 - over the range pagination numbers in URL producing a blank page

I did a good search before posting - but cannot figure out a way to prevent users from manually inputting page numbers in the URL of the paginator (a number over that range producing a blank page).

For example page 2 would have the following link: http://localhost/articles?page=2 If I have a maximum of 3 pages, and manually changing 2 into 5 within the URL, this will be redirecting to a blank page.

Not the end of the world, but surely annoying. I have looked at my options:

1 - Using isEmpty in the controller, after the paginator is called:

    if ($articles->isEmpty()) {
        return App::abort(404);
    }

This seemed to work fine at first (in terms of over the range pagination). But then I have discovered that when there are no articles, a 404 is thrown in my index page, which was not my intention.

2 - Use Ajax pagination, where there is no page number in the URI (ex. http://localhost/articles). This also requires a javaScript paginator, and I will not be able to use Laravel's. With Laravel, I am using simplePaginate, and could not find a similar one in javaScript (all I have found were full-blown, long paginators).

Any pointers on this?

0 likes
5 replies
jlrdw's avatar

I believe a bug was fixed, do you have the most up to date version, check the issues on this.

gabriel007's avatar

Good idea, will have a look, thanks.

Strange, pagination is one of the most used features, yet not much info on this... Even a Headers inspection will show Status Code:200 OK when using over the range pagination.

hlgrrnhrdt's avatar

See it like this, the page and perPage arguments are transfered to limit and offset in the SQL query. MySQL will not throw an error, when there is nothing to return for the given limit and offset.

I think this is something more for the developer to handle.

One way you already mentioned by responding with 404.

Or you have a page in place which tells the user that there is no result on that page and offer him some solution to that. Like pointing to the first or last page.

Or you redirect instantly to the first or last page with results.

Or see it as an edge case that might not be handled, because who changes the page parameter in the URL directly. Which of course depends on the target group of your website.

Hope this helps you.

power's avatar

I solved this problem with a custom middleware!

1.create a middleware

2.put your logic in the handle method.for example follow code in below section :

    public function handle($request, Closure $next)
    {
        $pagesCount = Post::paginate(10)->lastPage();
        if ($request->input('page') > $pagesCount)
            abort(404);

        return $next($request);
    }

3.register your middleware in the kernel file.

rujul04's avatar

abort_unless($users->count(), 204);

This prevents redirection for over ranged page number. For wrong page number, It will keep you on the same page.

Please or to participate in this conversation.