About PHP Framework Laravel plug-in pagination implement custom paging method

Source: Internet
Author: User
Tags php framework
This article mainly introduces the PHP framework Laravel5.1 plug-in pagination implementation of custom paging related information, the need for friends can refer to the following

Laravel of the page is very convenient, in fact, it is very easy to expand, the following to do an example, expand the Paginate () and Simplepaginate () method, to achieve our custom page style, such as displaying "previous page" and "next page", instead of "" "and" ", Of course, the extension of the method master you can be unscrupulous to expand a page you want, such as jump to a page, pagination shows how many records, the current display of the range of records, etc. balabala ...

5.1 and 5.2 should be the same method, I use the 5.2 version here. The document tells us that the paginator corresponds to the Simplepaginate method of the query statement constructor and eloquent, while lengthawarepaginator is equivalent to the Paginate method. Then we still have to look at the source code, specifically how this paginate is to achieve 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 ();  } ......}

A Presenter instance is passed into render (), and the instantiated render method is called to implement the paging display. If not, call Bootstrapthreepresenter in render () and 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 $windo W * @return void */Public function __construct (paginatorcontract $paginator, Urlwindow $window = null) {$this-&    Gt;paginator = $paginator; $this->window = Is_null ($window)?  Urlwindow::make ($paginator): $window->get (); }/** * Determine if the UNderlying Paginator being presented have 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 Bootstrapthreepresenter implementation of the Presentercontract interface, render () is the real implementation of the paging display, the first parameter in the construction method Paginatorcontract is actually a Paginator We continue to look at Presentercontract, which is the Presenter interface, defines what methods need to be implemented

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 have pages to show.   *   * @return BOOL   *  /Public Function haspages ();}

which defines the render and Haspages methods required to implement

Well, now that we're clear, we're going to customize the display of pagination, so we're going to write our own Presenter to implement the render () and haspages () in the interface.

First of all, a simple implementation of a paginate (), showing the "previous page" and "Next page", the middle is a number of pagination examples.

The new file is as follows (personal habits)

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 $windo W * @return void */Public function __construct (paginatorcontract $paginator, Urlwindow $window = null) {$this-&    Gt;paginator = $paginator; $this->window = Is_null ($window)?  Urlwindow::make ($paginator): $window->get (); }/** * DetermineIf the underlying paginator being presented have 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    '),///specific implementation can see the method $this->getlinks (), $this->getnextbutton (' next page ')//specific implementation can see the method));  } return ';   }/** * Get HTML wrapper for an available page link. * * @param string $url * @param int $page * @param string|null $rel * @return String */protected function Geta Vailablepagewrapper ($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 C lass= "Disabled Hide" ><span> ". $text.  ' </span></li> ';   }/** * Get HTML wrapper for active text. * * @param string $text * @return String */protected function Getactivepagewrapper ($text) {return ' <li CLA Ss= "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 (); }}

As simple as this, the main is the render () method, if you need to modify the page style in the project, or to add a paging jump and other requirements as long as you override the HTML elements in the methods displayed in it, very flexible, in the blade template also need to fix it, such as our paginator called $ Users, the default paging display is this:

{!! $users->render () |}
Changes to our customized pagination display:

{!! with (new \app\foundations\pagination\customerpresenter ($categories))->render ()!!}
OK, so on the page should be able to see the paging link contains "previous page" and "next page" plus the number of styles.

So what if the extension simplepaginate? is very simple, as long as the inheritance of the customerpresenter, to achieve haspages and render, as to why you can follow my view of the source of the way to see it, such as we changed to "previous" 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 (Pagi  Natorcontract $paginator) {$this->paginator = $paginator;   }/** * Determine if the underlying paginator being presented have 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 (' prev '), $this->getnextbutton (' next article ')));  } return '; }}

Pagination Display:

{!! with (new \app\foundations\pagination\customersimplepresenter ($categories))->render ()!!}

The method is this method, the specific modification according to their own needs to rewrite the corresponding display HTML elements of the method on it.

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.