Laravel5.1 + Pagination parsing and extension

Source: Internet
Author: User
Laravel5.1 + Pagination: it is very convenient to parse and expand the pages of Laravel. In fact, it is quite easy to expand. let's take an example to extend the paginate () and simplePaginate () methods, to customize the paging style, such as display "Previous Page"And "Next page"Instead "《"And """Of course, if you have mastered the extension method, you can expand the page you want without any Scrubs. for example, you can jump to a page and display the total number of records by page, the currently displayed record range 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
 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
 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
     

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

  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
      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

    If you think this article is very helpful to you, why not give it a try?

    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.