Laravel's pagination is very convenient, but it is actually quite easy to expand. Let's make an example below, expand the paginate() and simplePaginate() methods to implement our custom paging style, such as displaying "Previous Page" and "Next Page" instead of "" and "". Of course, if you master the expansion method, you can unscrupulously expand a paging you want, such as jumping to a certain page, the total number of records displayed on the paginate, the current record range, etc.
5.1 and 5.2 should be the same method, I am using the 5.2 version here. The documentation tells us that the Paginator corresponds to the query statement constructor and Eloquent's simplePaginate method, while the LengthAwarePaginator is equivalent to the paginate method. Then let's take a look at the source code, how to implement render() in paginate,
Illuminate/Pagination/
<?php namespace Illuminate\Pagination; ...... class LengthAwarePaginator extends AbstractPaginator implements Arrayable, ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Jsonable, LengthAwarePaginatorContract { ...... public function render(Presenter $presenter = null) { if (is_null($presenter) && static::$presenterResolver) { $presenter = call_user_func(static::$presenterResolver, $this); } $presenter = $presenter ?: new BootstrapThreePresenter($this); return $presenter->render(); } ...... }
What is passed in render() is an instance of Presenter and the instantiated render method is called to realize the display of pagination. If not, call render() in BootstrapThreePresenter. Let's see what BootstrapThreePresenter does.
Illuminate/Pagination/
<?php namespace Illuminate\Pagination; use Illuminate\Support\HtmlString; use Illuminate\Contracts\Pagination\Paginator as PaginatorContract; use Illuminate\Contracts\Pagination\Presenter as PresenterContract; class BootstrapThreePresenter implements PresenterContract { use BootstrapThreeNextPreviousButtonRendererTrait, UrlWindowPresenterTrait; /** * The paginator implementation. * * @var \Illuminate\Contracts\Pagination\Paginator */ protected $paginator; /** * The URL window data structure. * * @var array */ protected $window; /** * Create a new Bootstrap presenter instance. * * @param \Illuminate\Contracts\Pagination\Paginator $paginator * @param \Illuminate\Pagination\UrlWindow|null $window * @return void */ public function __construct(PaginatorContract $paginator, UrlWindow $window = null) { $this->paginator = $paginator; $this->window = is_null($window) ? UrlWindow::make($paginator) : $window->get(); } /** * Determine if the underlying paginator being presented has pages to show. * * @return bool */ public function hasPages() { return $this->paginator->hasPages(); } /** * Convert the URL window into Bootstrap HTML. * * @return \Illuminate\Support\HtmlString */ public function render() { if ($this->hasPages()) { return new HtmlString(sprintf( '<ul class="pagination">%s %s %s</ul>', $this->getPreviousButton(), $this->getLinks(), $this->getNextButton() )); } return ''; } ...... }
Here you can see that BootstrapThreePresenter implements the PresenterContract interface. render() is the real implementation of pagination display. The first parameter in the construction method PaginatorContract is actually a Paginator. Let's continue to look at what methods are defined in the Presenter interface that need to be implemented.
illuminate/contracts/Pagination/
<?php namespace Illuminate\Contracts\Pagination; interface Presenter { /** * Render the given paginator. * * @return \Illuminate\Contracts\Support\Htmlable|string */ public function render(); /** * Determine if the underlying paginator being presented has pages to show. * * @return bool */ public function hasPages(); }
The render and hasPages methods are defined to implement
Okay, then we are now very clear. If we want to customize the display of the pagination, we need to write our own Presenter to implement render() and hasPages() in the interface.
First, let’s simply implement a paginate() to display the “Previous Page” and “Next Page”, with examples of pagination numbers in the middle.
Create a new file as follows (personal habits)
app/Foundations/Pagination/
<?php namespace App\Foundations\Pagination; use Illuminate\Contracts\Pagination\Presenter as PresenterContract; use Illuminate\Contracts\Pagination\LengthAwarePaginator as PaginatorContract; use Illuminate\Pagination\UrlWindow; use Illuminate\Support\HtmlString; use Illuminate\Pagination\BootstrapThreeNextPreviousButtonRendererTrait; use Illuminate\Pagination\UrlWindowPresenterTrait; class CustomerPresenter implements PresenterContract { use BootstrapThreeNextPreviousButtonRendererTrait, UrlWindowPresenterTrait; protected $paginator; protected $window; /** * Create a new Bootstrap presenter instance. * * @param \Illuminate\Contracts\Pagination\Paginator $paginator * @param \Illuminate\Pagination\UrlWindow|null $window * @return void */ public function __construct(PaginatorContract $paginator, UrlWindow $window = null) { $this->paginator = $paginator; $this->window = is_null($window) ? UrlWindow::make($paginator) : $window->get(); } /** * Determine if the underlying paginator being presented has pages to show. * * @return bool */ public function hasPages() { return $this->paginator->hasPages(); } /** * Convert the URL window into Bootstrap HTML. * * @return \Illuminate\Support\HtmlString */ public function render() { if ($this->hasPages()) { return new HtmlString(sprintf( '<ul class="pagination">%s %s %s</ul>', $this->getPreviousButton('Previous page'),//You can view this method in detail $this->getLinks(), $this->getNextButton('Next Page')//You can view this method in detail )); } return ''; } /** * Get HTML wrapper for an available page link. * * @param string $url * @param int $page * @param string|null $rel * @return string */ protected function getAvailablePageWrapper($url, $page, $rel = null) { $rel = is_null($rel) ? '' : ' rel="' . $rel . '"'; return '<li><a href="' . htmlentities($url) . '"' . $rel . '>' . $page . '</a></li>'; } /** * Get HTML wrapper for disabled text. * * @param string $text * @return string */ protected function getDisabledTextWrapper($text) { return '<li class="disabled hide"><span>' . $text . '</span></li>'; } /** * Get HTML wrapper for active text. * * @param string $text * @return string */ protected function getActivePageWrapper($text) { return '<li class="active"><span>' . $text . '</span></li>'; } /** * Get a pagination "dot" element. * * @return string */ protected function getDots() { return $this->getDisabledTextWrapper('...'); } /** * Get the current page from the paginator. * * @return int */ protected function currentPage() { return $this->paginator->currentPage(); } /** * Get the last page from the paginator. * * @return int */ protected function lastPage() { return $this->paginator->lastPage(); } }
It's that simple, mainly the render() method. If you need to modify the pagination style in the project, or add pagination jumps, just rewrite the html elements in the method displayed in each item. It's very flexible and you also need to fix it in the blade template. For example, our Paginator is called $users, and the default pagination display is as follows:
{!! $users->render() !!}
Modify it to our customized page display:
{!! with(new \App\Foundations\Pagination\CustomerPresenter($categories))->render() !!}
OK, so you should see the styles of "Previous Page" and "Next Page" plus numbers in the page.
So if you extend simplePaginate? It is actually very simple. Just inherit the CustomerPresenter just now and implement hasPages and render. As for why you can read it according to the way I view the source code above, for example, we will change it to "Previous article" and "Next article"
Create a new App\Foundations\Pagination\
<?php namespace App\Foundations\Pagination; use Illuminate\Support\HtmlString; use Illuminate\Contracts\Pagination\Paginator as PaginatorContract; class CustomerSimplePresenter extends CustomerPresenter { /** * Create a simple Bootstrap 3 presenter. * * @param \Illuminate\Contracts\Pagination\Paginator $paginator * @return void */ public function __construct(PaginatorContract $paginator) { $this->paginator = $paginator; } /** * Determine if the underlying paginator being presented has pages to show. * * @return bool */ public function hasPages() { return $this->paginator->hasPages() && count($this->paginator->items()) > 0; } /** * Convert the URL window into Bootstrap HTML. * * @return \Illuminate\Support\HtmlString */ public function render() { if ($this->hasPages()) { return new HtmlString(sprintf( '<ul class="pager">%s %s</ul>', $this->getPreviousButton('Previous article'), $this->getNextButton('Next article') )); } return ''; } }
Pagination display:
{!! with(new \App\Foundations\Pagination\CustomerSimplePresenter($categories))->render() !!}
This is the method. Just modify and rewrite the corresponding method of displaying html elements according to your own needs.
Please indicate the reprint: Reprinted from Ryan is a rookie | LNMP Technology Stack Notes
The above is all about this article, I hope it will be helpful for everyone to learn PHP programming.