Home >Backend Development >PHP Tutorial >PHP framework Laravel plug-in Pagination implements custom paging, laravelpagination_PHP tutorial

PHP framework Laravel plug-in Pagination implements custom paging, laravelpagination_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:50:31898browse

PHP framework Laravel plug-in Pagination implements custom paging, laravelpagination

Laravel’s paging is very convenient, and in fact it is also quite easy to extend. Let’s do an example below to expand paginate () and simplePaginate() methods to implement our custom paging style, such as displaying "previous page" and "next page" instead of """ and """. Of course, you can do whatever you want after mastering the extension method. Expand a paging you want, such as jumping to a certain page, paging to display the total number of records, the currently displayed 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 Paginator corresponds to the query builder and Eloquent's simplePaginate method, while LengthAwarePaginator is equivalent to the paginate method. Then let’s take a look at the source code to see how paginate implements render(),

Illuminate/Pagination/LengthAwarePaginator.php

<&#63;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 &#63;: 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 paging display. If not, call render() in BootstrapThreePresenter to see what BootstrapThreePresenter does

Illuminate/Pagination/BootstrapThreePresenter.php

<&#63;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) &#63; 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 interface of PresenterContract. render() is the real implementation of paging display. The first parameter PaginatorContract in the construction method is actually a Paginator. Let’s continue to look at PresenterContract, which is defined in the Presenter interface. What methods need to be implemented

illuminate/contracts/Pagination/Presenter.php

<&#63;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 and need to be implemented

Okay, now we are very clear. If we want to customize the display of paging, we need to write our own Presenter to implement render() and hasPages() in the interface.

First, let's simply implement a paginate() to display "previous page" and "next page", with an example of pagination numbers in the middle.

The new file is as follows (personal habit)

app/Foundations/Pagination/CustomerPresenter.php

<&#63;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) &#63; 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 '';
  }

  /**
   * 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) &#63; '' : ' 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 paging style or add paging jumps in the project, just rewrite the html elements in each display method. It’s very easy. It is flexible and needs to be fixed in the blade template. For example, our Paginator is called $users. The default pagination display is like this:

{!! $users->render() !!}
Modified to our customized paging display:

{!! with(new AppFoundationsPaginationCustomerPresenter($categories))->render() !!}
Okay, now you should be able to see the pagination links containing the styles of "previous page" and "next page" plus numbers on the page.

So what if you extend simplePaginate? It's actually very simple. You just need to inherit the CustomerPresenter just now and implement hasPages and render. As for why, you can just look at it the way I viewed the source code above. For example, we change it to "previous article" and " Next article"

New AppFoundationsPaginationCustomerSimplePresenter.php

<&#63;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('上一篇'),
        $this->getNextButton('下一篇')
      ));
    }

    return '';
  }

}

Page display:

{!! with(new AppFoundationsPaginationCustomerSimplePresenter($categories))->render() !!}

The method is this method. Just modify it and rewrite the corresponding method of displaying html elements according to your own needs.

Please indicate when reprinting: Reprinted from Ryan is a rookie | LNMP technology stack notes

The above is the entire content of this article. I hope it will be helpful to everyone in learning PHP programming.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1133117.htmlTechArticlePHP framework Laravel plugin Pagination implements custom paging, laravelpagination Laravel’s paging is very convenient, and it is actually quite easy to expand. , let’s take an example and expand p...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn