Giter VIP home page Giter VIP logo

laravel-paginateroute's Introduction

๐Ÿšจ 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.

laravel-paginateroute's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-paginateroute's Issues

Laravel-paginateroute is not working in laravel 5.2 and page number is not increase.

Hi Team

Thanks for such a greate extension plugin. It is something that I am looking for.

I have followed the instruction on this page and installed the extension successfully, unfortunately it did not work for me.

My routes

Route::get('photo/', ['uses' => 'PhotoController@index']);

Route::get('photo/page/{page}', ['uses' => 'PhotoController@index']);

Method in my controller

public function index() {
$photos = Photo::orderBy('created_at', 'desc')->simplePaginate(15);

    $data['title'] = "SITE NAME | PHOTO";
    $data['page_content'] = 'YOU ARE VIEWING PHOTOS';
    $data['photos'] = $photos;

    if (view()->exists('photos.index')) {
        return view('photos.index', $data);
    } else {
        return 'view photos.index is not existed.';
    }

}

Use below lines to generate the URLs

@if(PaginateRoute::hasPreviousPage())
Previous
@endif
@if(PaginateRoute::hasNextPage($photos))
Next
@endif

image

I can see the next link, but nothing happen after I clicked on it.

After my first clicked, I noticed that it adds a new "page/2" to the end of the URL instead of change the page number from 2 to 3.

And also the page number doesn't increase ( the page number should increased from 2 to 3, but still show 2 ).

Below is the URL after my second click.

http://localhost/my-site-name/photo/page/2/page/2

Note: I am using laravel 5.2, and it is working fine if I use the default laravel pagination.

Any advices and helps are much appreciated

Thanks

Declaration of App\Providers\RouteServiceProvider::boot ...

I have this error :

ErrorException thrown with message "Declaration of App\Providers\RouteServiceProvider::boot(Illuminate\Routing\Router $router) should be compatible with Illuminate\Foundation\Support\Providers\RouteServiceProvider::boot()"

route function generate url error

use

Route::get('video_list', 'Video\ListController@index')->name('video.list.index');

<a href="{{ route('video.list.index',['sort'=>'recorded_at-desc']) }}">new </a>

result
<a href="http://xxx.dev/video_list?sort=created_at-desc">new</a>


use

Route::paginate('video_list', 'Video\ListController@index')->name('video.list.index');

<a href="{{ route('video.list.index',['sort'=>'recorded_at-desc']) }}">new </a>

result

<a href="http://xxx.dev/video_list/created_at-desc">new</a>

i redirect to this url 404

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 !

Generate full paginate

How to generate paginate with previous, numbers and next links in list?

<ul>
    <li><a href="http://example.com/news">Previous</a></li>
    <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">Next</a></li>
</ul>

Compatibility issue with Laravel 5.5 +

screenshot from 2018-08-14 23-53-14

After upgrading from Laravel 5.1 to 5.5, the package is generating an error for 'getURI' method. The package is really great and I would love to see it supporting current versions of laravel.

Upgrading core dependencies to 5.5 and Replacing the calls to getUri() method with $this->router->current()->uri should work. Just did it from the top of my head.

Would love to hear from you.

remove 'page' word from urls?

Currently page links are like this:
http://example.com/products/page/2

Is there a way to change it so it create links like this?
http://example.com/products/2

I believe it's more SEO friendly, and also doesn't need to be translated for different locales.
The page number is the last URL segment (always).

How to use pagitnation with Cache?

Hello,
How should I use pagination query with Cache?
The following query return same cached results

$blogs= Cache::remember('blogs', 720,function() {
            return Blog::with(['comments'])->simplePaginate(5);
        });

Regards

Custom path with get params

Hello, dear colleagues!

I'm trying to use your package in my project.
I have a catalog with paginated products and different filters which reloads catalog by ajax. Pagination links is regenerated every times when filters are applying. Url of catalog - "/catalog", filters sends ajax query on url "/catalog/get_products". Filters adds conditions to GET params of query.

So, methods previousUrl, nextUrl and pageUrl when filters applying returns links with url "/catalog/get_products".

I want to set custom path for generating this links. Method addPageQuery does not suit me because it ignore GET params in the query.

Now I fixed it with a little awful decision:

class MyPaginateRoute extends \Spatie\PaginateRoute\PaginateRoute
{
    public function pageUrlWithCustomPath($path, $page, $full = false)
    {
        /* ... */
    }

    public function previousPageUrlWithCustomPath($path, $full = false)
    {
        /* ... */
    }

    public function nextPageUrlWithCustomPath(\Illuminate\Contracts\Pagination\Paginator $paginator, $path)
    {
        /* ... */
    }

And my pagination template has next view:

@if ($paginator->hasPages())
    <ul class="pagination">
        {{-- Previous Page Link --}}
        @if (PaginateRoute::hasPreviousPage())
            <li><a href="{{ PaginateRoute::previousPageUrlWithCustomPath($paginationLink) }}" rel="prev">&laquo;</a></li>
        @else
            <li class="disabled"><span>&laquo;</span></li>
        @endif

        {{-- Pagination Elements --}}
        @foreach ($elements as $element)
            {{-- "Three Dots" Separator --}}
            @if (is_string($element))
                <li class="disabled"><span>{{ $element }}</span></li>
            @endif

            {{-- Array Of Links --}}
            @if (is_array($element))
                @foreach ($element as $page => $url)
                    @if ($page == PaginateRoute::currentPage())
                        <li class="active"><span>{{ $page }}</span></li>
                    @else
                        <li><a href="{{ PaginateRoute::pageUrlWithCustomPath($paginationLink, $page) }}">{{ $page }}</a></li>
                    @endif
                @endforeach
            @endif
        @endforeach

        {{-- Next Page Link --}}
        @if (PaginateRoute::hasNextPage($paginator))
            <li><a href="{{ PaginateRoute::nextPageUrlWithCustomPath($paginator, $paginationLink) }}" rel="next">&raquo;</a></li>
        @else
            <li class="disabled"><span>&raquo;</span></li>
        @endif
    </ul>
@endif

Can we make anything better then three additional methods for custom path? Maybe PaginateRoute should saving internal state? Something like this:

class PaginateRoute 
{
    private $path;

    public function setPath($path) 
    {
        $this->path = $path;
    }

    public function getPath() 
    {
        return $this->path ?? $this->router->getCurrentRoute()->uri();
    }

    public function pageUrl($page, $full = false)
    {
        $currentPageUrl = $this->getPath();
        /* ... */
    }
}

// USAGE IN VIEW

/* ... */
<?$paginator = app(PaginateRoute::class)->setPath($paginationLink); ?>
@foreach ($element as $page => $url)
    @if ($page == $paginator->currentPage())
        <li class="active"><span>{{ $page }}</span></li>
    @else
        <li>
            <a href="{{ $paginator->pageUrl($page) }}">{{ $page }}
            </a>
        </li>
    @endif
@endforeach
/* ... */

not static method

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

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;

More UI flexibility?

Hello, what do you think of adding more options to add class to li tag and a links? Let's say that some projects on bootstrap, materialize and others frameworks or custom design will add custom classes, but this package doesn't support this and they will need to hardcode it.

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.

Problem with laravel 5.2 and language it

I install your code in my application, everything goes right but when i change my locale language in to "it"
Argument 1 passed to Illuminate\Translation\FileLoader::loadNamespaceOverrides() must be of the type array, integer given, called in /home/vagrant/Code/XXXXX/vendor/laravel/framework/src/Illuminate/Translation/FileLoader.php on line 73 and defined

Conflicting with spatie/laravel-medialibrary

Hi,
I am trying to use spatie/laravel-medialibrary and spatie/laravel-paginateroute on the same project but when i try to install spatie/laravel-medialibrary it gives me following error,

Problem 1
- Can only install one of: spatie/string[2.0.0, 1.9.1].
- Can only install one of: spatie/string[2.0.0, 1.9.1].
- Can only install one of: spatie/string[2.0.0, 1.9.1].
- spatie/laravel-medialibrary dev-master requires spatie/string ^2.0 -> satisfiable by spatie/string[2.0.0].
- Installation request for spatie/laravel-medialibrary dev-master -> satisfiable by spatie/laravel-medialibrary[dev-master].
- Installation request for spatie/string == 1.9.1.0 -> satisfiable by spatie/string[1.9.1].

I checked and both the packages are using different versions of spatie/string

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

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

    renderPageList() doesn't include any query string

    I working on a search results page where each filter are submitting as GET method such as

    /search?min=100&max=5000

    However when using renderPageList() it strip out all of the query string instead of linking to

    /search/page/2?min=100&max=5000

    It links to /search/page/2

    Is there any reason of this or it's just something that have been missing from the function?

    Workaround is I add another variable to renderPageList()

    public function renderPageList(LengthAwarePaginator $paginator, $full = false, $class = null, $additionalLinks = false, $suffx = '') {
    .
    .
    $url .= $suffix;
    .
    .
    }
    

    The question is I'm not sure is this the correct way in solving this problem?

    Recommend Projects

    • React photo React

      A declarative, efficient, and flexible JavaScript library for building user interfaces.

    • Vue.js photo Vue.js

      ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

    • Typescript photo Typescript

      TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

    • TensorFlow photo TensorFlow

      An Open Source Machine Learning Framework for Everyone

    • Django photo Django

      The Web framework for perfectionists with deadlines.

    • D3 photo D3

      Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

    Recommend Topics

    • javascript

      JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

    • web

      Some thing interesting about web. New door for the world.

    • server

      A server is a program made to process requests and deliver data to clients.

    • Machine learning

      Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

    • Game

      Some thing interesting about game, make everyone happy.

    Recommend Org

    • Facebook photo Facebook

      We are working to build community through open source technology. NB: members must have two-factor auth.

    • Microsoft photo Microsoft

      Open source projects and samples from Microsoft.

    • Google photo Google

      Google โค๏ธ Open Source for everyone.

    • D3 photo D3

      Data-Driven Documents codes.