PHP framework Laravel plug-in Pagination to implement custom paging. laravelpagination_PHP tutorial

Source: Internet
Author: User
Tags learn php programming
The Laravel plug-in Pagination in the PHP framework implements custom paging and laravelpagination. The Laravel plug-in the PHP framework Pagination implements custom paging. the Pagination of laravelpaginationLaravel is very convenient, but it is also very easy to expand. The following is an example, expand the p PHP framework Laravel plug-in Pagination to implement custom paging, laravelpagination

Laravel paging is very convenient, but it is quite easy to expand. let's take an example below to extend the paginate () and simplePaginate () methods to implement custom paging styles, for example, if "previous page" and "next page" are displayed, instead of "" and ", you can expand the page you want without authorization, for example, you can jump to a page, display the total number of records by page, the range of currently displayed records, and so on...

5.1 and 5.2 should be the same method. I use 5.2 here. The document tells us that Paginator corresponds to the simplePaginate method of the query statement constructor and Eloquent, while LengthAwarePaginator is equivalent to the paginate method. Let's take a look at the source code. how does paginate implement render,

Illuminate/Pagination/LengthAwarePaginator. php

<?phpnamespace 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();  }......}

In render (), a Presenter instance is input, and the instantiated render method is called to display pages. If not, call render () in BootstrapThreePresenter to see what BootstrapThreePresenter is doing.

Illuminate/Pagination/BootstrapThreePresenter. php

<?phpnamespace 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(        '
 
 
    %s %s %s
', $this->getPreviousButton(), $this->getLinks(), $this->getNextButton() )); } return ''; }......}

Here we can see that BootstrapThreePresenter implements the PresenterContract interface, and render () is the real implementation of paging display, the first parameter PaginatorContract in the constructor is actually a Paginator. let's continue to look at what methods need to be implemented in the PresenterContract interface.

Illuminate/contracts/Pagination/Presenter. php

<?phpnamespace 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 need to be implemented.

Now we are very clear. to customize the display of pages, we need to write our own Presenter to implement render () and hasPages () in the interface () you can.

First, we will simply implement a paginate () to display "previous" and "next", with an example of paging numbers in the middle.

Create a file as follows (personal habits)

App/Foundations/Pagination/CustomerPresenter. php

<? Php namespace App \ Foundations \ Pagination; use Illuminate \ Contracts \ Pagination \ Presenter as alias; use Illuminate \ Contracts \ Pagination \ stored as alias; use Illuminate \ Pagination \ UrlWindow; use Illuminate \ Support \ HtmlString; use Illuminate \ Pagination \ bootstrapthreenextpreviusbuttonrenderertrait; use Illuminate \ Pagination \ UrlWindowPresenterTrait; class CustomerPres Enter implements PresenterContract {use bootstrapthreenextpreviusbuttonrenderertrait, 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, U RlWindow $ 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 ('
 
 
    % S
', $ This-> getpreviusbutton ('previous page'), // You can view the method for implementation. $ this-> getLinks (), $ this-> getNextButton ('next page ') // You can view the method for specific implementation);} 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'
  • '. $ Page .'
  • ';}/*** Get HTML wrapper for disabled text. ** @ param string $ text * @ return string */protected function getDisabledTextWrapper ($ text) {return'
  • '. $ Text .'
  • ';}/*** Get HTML wrapper for active text. ** @ param string $ text * @ return string */protected function getActivePageWrapper ($ text) {return'
  • '. $ Text .'
  • ';}/*** 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 ();}}

    This is simple, mainly the render () method. if you need to modify the paging style in the project, or, to add paging jump and other requirements, you only need to rewrite the html elements in each of the display methods, which is flexible and needs to be modified in the blade template, for example, if our Paginator is called $ users, the default page display is as follows:

    {!! $ Users-> render ()!!}
    Changed to the custom page display:

    {!! With (new \ App \ Foundations \ Pagination \ CustomerPresenter ($ categories)-> render ()!!}
    Now, we can see that the pagination link contains the "previous page" and "next page" plus numbers.

    So if simplePaginate is extended? In fact, it is very simple. just inherit the CustomerPresenter and implement hasPages and render. As for why can I check the source code above? for example, we can change it to "previous" and "next"

    Create 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 ('
     
     
      % S
    ', $ This-> getpreviusbutton ('preput'), $ this-> getNextButton ('next up');} return '';}}

    Pagination:

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

    This method is used. you can rewrite the corresponding html element display method as needed.

    Reprinted please note: Reprinted from Ryan is a Cainiao | LNMP technology stack notes

    The above is all the content of this article. I hope it will help you learn PHP programming.

    The paging of Laravel is very convenient, but it is quite easy to expand. let's take an example below to extend p...

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.