Laravel router extension to easily use Laravel's paginator without the query string

Overview

🚨 THIS PACKAGE HAS BEEN ABANDONED 🚨

We don't use this package anymore in our own projects and cannot justify the time needed to maintain it anymore. That's why we have chosen to abandon it. Feel free to fork our code and maintain your own copy.

Laravel Paginate Route

Latest Version on Packagist Software License Build Status Quality Score StyleCI Total Downloads

This package adds the paginate route method to support pagination via custom routes instead of query strings. This also allows for easily translatable pagination routes ex. /news/page/2, /nieuws/pagina/2.

Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Note: If you're upgrading to 2.0, check out the upgrade guide below.

Postcardware

You're free to use this package (it's MIT-licensed), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

The best postcards will get published on the open source page on our website.

Install

Via Composer

$ composer require spatie/laravel-paginateroute

First register the service provider and facade in your application.

// config/app.php

'providers' => [
    ...
    'Spatie\PaginateRoute\PaginateRouteServiceProvider',
];

'aliases' => [
    ...
    'PaginateRoute' => 'Spatie\PaginateRoute\PaginateRouteFacade',
];

Then register the macros in App\Providers\RouteServiceProvider::boot().

// app/Providers/RouteServiceProvider.php

use PaginateRoute;

// ...

public function boot()
{
    PaginateRoute::registerMacros();

    parent::boot();
}

Usage

The paginate route macro will register two routes for you.

// app/Http/routes.php

// Generates /users & /users/page/{page}
Route::paginate('users', 'UsersController@index');

In your route's action you can just use Laravel's regular pagination methods.

// app/Http/Controllers/UsersController.php

public function index()
{
    return view('users.index', ['users' => \App\User::simplePaginate(5)]);
}

If you want to customize or add translations for the "page" url segment, you can publish the language files.

$ php artisan vendor:publish --provider="Spatie\PaginateRoute\PaginateRouteServiceProvider"

Generating Url's

Since Laravel's paginator url's will still use a query string, PaginateRoute has it's own url generator and page helper functions.

{{-- $users is an instance of \Illuminate\Contracts\Pagination\Paginator --}}

@if(PaginateRoute::hasPreviousPage())
  <a href="{{ PaginateRoute::previousPageUrl() }}">Previous</a>
@endif

@if(PaginateRoute::hasNextPage($users))
  <a href="{{ PaginateRoute::nextPageUrl($users) }}">Next</a>
@endif

The nextPage functions require the paginator instance as a parameter, so they can determine whether there are any more records.

/**
 * @param  \Illuminate\Contracts\Pagination\Paginator $paginator
 * @return int|null
 */
public function nextPage(Paginator $paginator)
/**
 * @param  \Illuminate\Contracts\Pagination\Paginator $paginator
 * @return bool
 */
public function hasNextPage(Paginator $paginator)
/**
 * @param  \Illuminate\Contracts\Pagination\Paginator $paginator
 * @return string|null
 */
public function nextPageUrl(Paginator $paginator)
/**
 * @return int|null
 */
public function previousPage()
/**
 * @return bool
 */
public function hasPreviousPage()
/**
 * @param  bool $full
 * @return string|null
 */
public function previousPageUrl($full = false)
/**
 * @param int  $page
 * @param bool $full
 * @return string
 */
public function pageUrl($page, $full = false)

If $full is true, the first page will be a fully qualified url. Ex. /users/page/1 instead if just /users (this is the default).

To retrieve the url of a specific page of a paginated route, that isn't the current route, there's the addPageQuery function.

/**
 * @param string $url
 * @param int $page
 * @param bool $full
 * @return string
 */
public function addPageQuery($url, $page, $full = false)

You can also retrieve an array with all available urls. These can be rendered as a plain html list with page numbers. Note that these functions require a LengthAwarePaginator.

/**
 * @param  \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator
 * @param  bool $full
 * @return array
 */
public function allUrls(LengthAwarePaginator $paginator, $full = false)
/**
 * @param  \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator
 * @param  bool $full
 * @param  string $class
 * @param  bool $additionalLinks
 * @return string
 */
public function renderPageList(LengthAwarePaginator $paginator, $full = false, $class = null, $additionalLinks = false)
<!-- Example output: -->
<ul class="pagination">
    <li><a href="http://example.com/news">1</a></li>
    <li><a href="http://example.com/news/page/2">2</a></li>
    <li class="active"><a href="http://example.com/news/page/3">3</a></li>
    <li><a href="http://example.com/news/page/4">4</a></li>
    <li><a href="http://example.com/news/page/4">&raquo;</a></li>
</ul>

You can render link tags to mark previous and next page for SEO. Note that these functions require a LengthAwarePaginator.

/**
 * @param  \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator
 * @param  bool $full
 * @return string
 */
public function renderRelLinks(LengthAwarePaginator $paginator, $full = false)
<!-- Example output: -->
<link rel="prev" href="http://example.com/news/page/2" />
<link rel="next" href="http://example.com/news/page/4" />

Tests

The package contains some integration/smoke tests, set up with Orchestra. The tests can be run via phpunit.

$ phpunit

Upgrading

1.x => 2.0

The 2.0 release changes the route macro to only register one route with the entire query in it, so providing a page parameter to the action link is no longer possible.

For example, action('FooController@bar', ['page' => 3]) is no longer possible, and should be replaced by PaginateRoute::addPageQuery(action('FooController@bar'), 3).

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

About Spatie

Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

License

The MIT License (MIT). Please see License File for more information.

Comments
  • Class 'PaginateRoute' not found

    Class 'PaginateRoute' not found

    I'm using laravel 5.4 and after I put PaginateRoute::registerMacros(); into my RouteServiceProvider.php i got this error: Class 'PaginateRoute' not found

    Of course I pasted use PaginateRoute;

    opened by noge075 10
  • Too many subpages, shattering layout.

    Too many subpages, shattering layout.

    I facilitate the pagination with great joy, but when my dataset gets to large, I experience too many subpages, resulting in a pagination list that exceeds the borders in my layout. Is it in anyway possible to reduce the number og subpages and use dots instead? Like: 1 2 3 4 5 ... 56?

    Regards.

    opened by fredemagi 8
  • Problem with routes

    Problem with routes

    Hey!

    I have a problem with routes in Laravel 5.3:

    Route: Route::get('/genres/{alt}', ['as' => 'genres.show', 'uses' => 'GenreController@show']); Route::paginate('/genres', [ 'as' => 'genres.main', 'uses' => 'GenreController@index' ] );

    So link http://mysite.com/genres/xxxx - working fine, but links like http://mysite.com/genres/xxxx/page/x - are 404 error, but links like http://mysite.com/genres/xxxx?page=x - working fine.

    How to fix http://mysite.com/genres/xxxx/page/x ?

    opened by sh241 8
  • Outdated docs! Non-static method Spatie\PaginateRoute\PaginateRoute::registerMacros() should not be called statically

    Outdated docs! Non-static method Spatie\PaginateRoute\PaginateRoute::registerMacros() should not be called statically

    read your code, then read the docs. It won't work....

    Non-static method Spatie\PaginateRoute\PaginateRoute::registerMacros() should not be called statically

    opened by torian257x 6
  • Error Class 'Illuminate\Routing\RouteParameterBinder' not found after update from 2.2.2 to 2.4.1

    Error Class 'Illuminate\Routing\RouteParameterBinder' not found after update from 2.2.2 to 2.4.1

    Hello,

    just as information, the package generate a error after updating from 2.2.2 to 2.4.1 on Laravel 5.1. Error is Class 'Illuminate\Routing\RouteParameterBinder' not found.

    Switching back to 2.2.2 fix the issue.

    bug help wanted 
    opened by mtx-z 5
  • Translation of

    Translation of "/page/"

    Hey there ! Thanks for the great package.

    I have an issue with the translation of the "page" segment. The "page" word is never translated, stay the same in every language. I published translation file for paginate-route (resources/lang/vendors/paginateroute) and edited them. I re-published multiple times already to be sure.

    My route is declared like so: Route::paginate('/', ['as' => 'home', 'uses' => 'FrontendController@index']);

    If I try change the URL in my browser from domain.com/en/page/2 to domain.com/de/seite/2, it goes 404. So the URL with translated "page" does not even exist.

    Anything i could have forgotten to do ?

    Thanks in advance !

    opened by mtx-z 5
  • Hide extra pages

    Hide extra pages

    Thanks again for the great code! Perhaps I am missing something. Is there a way to have the same visual has the one originally generated by laravel? I mean, When you have 150 results with the original paginator from laravel, and you decide to split in 10 records per page, the original laravel paginator decides to hide a couple from previous pages or next pages. Eg: Original Laravel on page 8: << 1 2 3 ... 7 8 9 ... 13 14 15 >>

    With spatie: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

    Also to decide to place a class or ID to the main

      generated would be optimal.

      Thanks

      opened by bernardomacedo 5
    • non-static methods

      non-static methods

      Hi guys,

      I am really surprised, but php complains about non-static methods.

      public function boot() { PaginateRoute::registerMacros();

          parent::boot();
      }
      

      this method is not static

      opened by aidenko 4
    • Links generate question marks

      Links generate question marks

      When I use

      PaginateRoute::pageUrl($i)

      this not generate full url examle: http://example.com/news/page/2 byt only "?".

      And this happens even when I use other url generator and page helper functions.

      opened by michaloravec 4
    • Gil 1 patch 1 - Simple path

      Gil 1 patch 1 - Simple path

      I loved the package but wanted to use it with a url like this :

      users/2 instead of users/page/2

      I would love to contribute. Please let me know if I need to change or fix something. It's the first time I propose modification to a php package.

      I remade the PR with the StyleCI fixed.

      opened by Gil-1 4
    • Improve pagination markup to honor 5.7 onEachSide()

      Improve pagination markup to honor 5.7 onEachSide()

      This improvement will limit how many links markup should be displayed instead of just showing every links. The limit will honor 5.7 onEachSide() value

      opened by duckzland 3
    Releases(2.7.0)
    Owner
    Spatie
    We create open source, digital products and courses for the developer community
    Spatie
    A lightweight PHP paginator, for generating pagination controls in the style of Stack Overflow and Flickr.

    PHP Paginator A lightweight PHP paginator, for generating pagination controls in the style of Stack Overflow and Flickr. The "first" and "last" page l

    Jason Grimes 370 Dec 21, 2022
    Composer package which adds support for HTML5 elements using Laravels Form interface (e.g. Form::date())

    Laravel HTML 5 Inputs Composer package which adds support for HTML5 elements by extending Laravel's Form interface (e.g. Form::date()) Adds support fo

    Small Dog Studios 11 Oct 13, 2020
    A package to flash multiple messages using Laravels default session message flashing system

    Flash multiple advanced messages with both text, messages and links An opinionated solution for flashing multiple advanced messages from the backend a

    Bilfeldt 6 Jan 18, 2022
    Adds a way to write php and run it directly in Laravels' Artisan Tinker.

    Adds a way to write php in PhpStorm/IDEA and run it directly as if through laravel artisan tinker - allowing you to quickly run a piece of code with a

    Robbin 120 Jan 2, 2023
    Stop duplicating your Eloquent query scopes and constraints in PHP. This package lets you re-use your query scopes and constraints by adding them as a subquery.

    Laravel Eloquent Scope as Select Stop duplicating your Eloquent query scopes and constraints in PHP. This package lets you re-use your query scopes an

    Protone Media 75 Dec 7, 2022
    A simple to use query builder for the jQuery QueryBuilder plugin for use with Laravel.

    QueryBuilderParser Status Label Status Value Build Insights Code Climate Test Coverage QueryBuilderParser is designed mainly to be used inside Laravel

    Tim Groeneveld 149 Nov 11, 2022
    A Laravel Wrapper for the CoinDCX API. Now easily connect and consume the CoinDCX Public API in your Laravel apps without any hassle.

    This package provides a Laravel Wrapper for the CoinDCX API and allows you to easily communicate with it. Important Note This package is in early deve

    Moinuddin S. Khaja 2 Feb 16, 2022
    Use Laravel's built-in ORM classes to query cloud resources with Steampipe.

    Laravel Steampipe Use Laravel's built-in ORM classes to query cloud resources with Steampipe, an open source CLI to instantly query cloud APIs using S

    Renoki Co. 13 Nov 8, 2022
    A query database collection for use with Laravel Pipeline

    A query database collection for use with Laravel Pipeline This package contains a collection of class that can be used with Laravel Pipeline. Let's se

    Dương Gia Bảo 188 Dec 24, 2022
    Use Blade templates without the full Laravel framework

    blade Use Laravel Blade templates as a standalone component without the full Laravel framework Full documentation is available at http://duncan3dc.git

    Craig Duncan 138 Dec 7, 2022
    Use ESM with importmap to manage modern JavaScript in Laravel without transpiling or bundling

    Introduction Use ESM with importmap to manage modern JavaScript in Laravel without transpiling or bundling. Inspiration This package was inspired by t

    Tony Messias 91 Dec 30, 2022
    Simplest Slugify for PHP to convert string into a slug.

    Simplest Slugify for PHP to convert string into a slug. Documentation You can find the detailed documentation here in Slugify Documentation. Contribut

    Pharaonic 6 Mar 12, 2022
    Get estimated read time of an article. Similar to medium.com's "x min read". Multilingual including right-to-left written languages. Supports JSON, Array and String output.

    Read Time Calculates the read time of an article. Output string e.g: x min read or 5 minutes read. Features Multilingual translations support. Static

    Waqar Ahmed 8 Dec 9, 2022
    A package to easily make use of Iconic icons in your Laravel Blade views.

    Blade Iconic A package to easily make use of Iconic icons in your Laravel Blade views. For a full list of available icons see the SVG directory. Iconi

    Malik Alleyne-Jones 17 Aug 25, 2022
    A package to easily make use of Simple Icons in your Laravel Blade views.

    Blade Simple Icons A package to easily make use of Simple Icons in your Laravel Blade views. For a full list of available icons see the SVG directory.

    UB Labs 12 Jan 17, 2022
    Zarinpal is a laravel package to easily use zarinpal.com payment services in your applications

    پکیج اتصال به درگاه پرداخت زرین پال zarinpal.com برای اتصال به درگاه پرداخت اینترنتی زرین پال و استفاده از api های آن می توانید از این پکیج استفاده کن

    Rahmat Waisi 4 Jan 26, 2022
    A package to easily make use of SVG icons in your Laravel Blade views.

    Blade Icons A package to easily make use of SVG icons in your Laravel Blade views. Originally "Blade SVG" by Adam Wathan. Turn... <!-- camera.svg -->

    Blade UI Kit 1.7k Jan 2, 2023
    Laravel Setting - Easily save, update and get titles, descriptions, and more. it is very easy to use.

    Laravel Setting Easily save, update and get titles, descriptions, and more. it is very easy to use. This is great for storing and receiving general si

    Ali Ranjbar 2 Aug 23, 2022
    A package to easily make use of Iconsax in your Laravel Blade views.

    Blade Iconsax A package to easily make use of Iconsax in your Laravel Blade views. This package contains 1.000 icons in 6 diferent styles, a total of

    Guilherme Saade 4 Oct 22, 2022