Home > Backend Development > PHP Tutorial > Laravel uses the Pagination plug-in to implement custom pagination

Laravel uses the Pagination plug-in to implement custom pagination

*文
Release: 2023-03-18 20:52:01
Original
2124 people have browsed it

How does Laravel use the Pagination plug-in to implement custom pagination? This article mainly introduces the relevant information about the PHP framework Laravel5.1 plug-in Pagination to implement custom paging. Friends in need can refer to it. I hope to be helpful.

Laravel's pagination is very convenient. In fact, it is also very easy to extend. Let's do an example below to extend the paginate() and simplePaginate() methods to implement our custom paging style, such as displaying "Up "One page" and "Next page" instead of """ and """. Of course, if you master the expansion method, you can expand a paging you want without restraint, such as jumping to a certain page and displaying the paging. How many records are there in total, the range of records currently displayed, and so on. . .

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. Specifically, how does paginate implement render()?

Illuminate/Pagination/LengthAwarePaginator.php

<?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();
  }
......
}
Copy after login

render() What is entered is an instance of Presenter, and the render method of this instantiation is called to realize paging display. If not, call render() in BootstrapThreePresenter to see what BootstrapThreePresenter does

Illuminate/Pagination/BootstrapThreePresenter.php

<?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(
        &#39;<ul class="pagination">%s %s %s</ul>&#39;,
        $this->getPreviousButton(),
        $this->getLinks(),
        $this->getNextButton()
      ));
    }

    return &#39;&#39;;
  }
......
}
Copy after login

You can see BootstrapThreePresenter here After implementing 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 what methods are defined in the Presenter interface and need to be implemented.

illuminate/contracts/Pagination/Presenter.php


<?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();
}
Copy after login

The render and hasPages methods are defined and need to be implemented

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

First of all, 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

<?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(
        &#39;<ul class="pagination">%s %s %s</ul>&#39;,
        $this->getPreviousButton(&#39;上一页&#39;),//具体实现可以查看该方法
        $this->getLinks(),
        $this->getNextButton(&#39;下一页&#39;)//具体实现可以查看该方法
      ));
    }

    return &#39;&#39;;
  }

  /**
   * 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) ? &#39;&#39; : &#39; rel="&#39; . $rel . &#39;"&#39;;

    return &#39;<li><a href="&#39; . htmlentities($url) . &#39;"&#39; . $rel . &#39;>&#39; . $page . &#39;</a></li>&#39;;
  }

  /**
   * Get HTML wrapper for disabled text.
   *
   * @param string $text
   * @return string
   */
  protected function getDisabledTextWrapper($text)
  {
    return &#39;<li class="disabled hide"><span>&#39; . $text . &#39;</span></li>&#39;;
  }

  /**
   * Get HTML wrapper for active text.
   *
   * @param string $text
   * @return string
   */
  protected function getActivePageWrapper($text)
  {
    return &#39;<li class="active"><span>&#39; . $text . &#39;</span></li>&#39;;
  }

  /**
   * Get a pagination "dot" element.
   *
   * @return string
   */
  protected function getDots()
  {
    return $this->getDisabledTextWrapper(&#39;...&#39;);
  }

  /**
   * 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();
  }

}
Copy after login

It’s that simple, the main thing is render() Method, if you need to modify the paging style in the project, or add pagination jumps and other requirements, you only need to rewrite the html elements in each displayed method. It is very flexible and needs to be modified in the blade template, such as Our Paginator is called $users, and the default paging display is like this:

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

{!! with(new \App\Foundations\Pagination\CustomerPresenter($categories))->render() !!}Okay, this should be enough on the page You can see that the pagination link contains the styles of "previous page" and "next page" plus numbers.

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 App\Foundations\Pagination\CustomerSimplePresenter.php

<?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(
        &#39;<ul class="pager">%s %s</ul>&#39;,
        $this->getPreviousButton(&#39;上一篇&#39;),
        $this->getNextButton(&#39;下一篇&#39;)
      ));
    }

    return &#39;&#39;;
  }

}
Copy after login

Pagination display:

{!! with(new \App\Foundations\Pagination\CustomerSimplePresenter($categories))->render() !!}

The method is this method, the specific modifications are according to your own You just need to rewrite the corresponding method of displaying html elements.

Related recommendations:

An elegant way to generate Gravatar avatar address using Laravel

Detailed explanation of Laravel reset password example

laravel writing APP interface (API)

The above is the detailed content of Laravel uses the Pagination plug-in to implement custom pagination. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template