PHP Framework Laravel plugin pagination implement custom paging _php tips

Source: Internet
Author: User
Tags learn php learn php programming php framework php programming prev sprintf wrapper

Laravel is convenient for paging, in fact, it's easy to expand, and here's an example to extend the Paginate () and Simplepaginate () methods to implement our custom pagination style, such as "Prev" and "next" instead of "and", Of course, the extension of the method mastered you can unbridled expansion of a page you want, such as jump to a page, page shows how many records, the current display of the record range and so Balabala ...

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

A presenter instance is passed in render (), and the instantiated render method is invoked to implement the paging display. If not, call Bootstrapthreepresenter in render () to see what Bootstrapthreepresenter is for.

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 $win
    Dow * @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 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 ()) {R Eturn New Htmlstring (sprintf (' <ul class= "pagination" >%s%s%s</ul> '), $this->getpreviousbut
    Ton (), $this->getlinks (), $this->getnextbutton ()));
  } return ';

 }
......
}

Here you can see that Bootstrapthreepresenter implements the Presentercontract interface, render () is the real implementation of pagination display, the first parameter in the construction method Paginatorcontract is actually a Paginator let's continue to look at the presentercontract, which is defined in the presenter interface to implement

illuminate/contracts/pagination/presenter.php

<?php

namespace Illuminate\contracts\pagination;

Interface presenter 
{
  /**
   * Render given paginator.
   *
   * @return \illuminate\contracts\support\htmlable|string
   *
  /Public function render ();

  /**
   * Determine if the underlying paginator being presented pages to show.
   *
   * @return BOOL
   *
  /Public Function haspages ();
}

which defines the render and haspages methods that need to be implemented

Well, now that we're clear, we're going to customize the paging display, so it's time to write our own presenter to implement the Render () and Haspages () on the interface.

The first step is to simply implement a paginate (), showing "prev" and "Next", with examples of pagination numbers in the middle.

Create a new file 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 $win
    Dow * @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 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 ()) {R Eturn New Htmlstring (sprintf (' <ul class= "pagination" >%s%s%s</ul> '), $this->getpreviousbut
    ton (' prev '),//specific implementation can view the method $this->getlinks (), $this->getnextbutton (' next page ')//specific implementation can view the method));
  } return ';
   /** * Get HTML wrapper for a available page link. * * @param string $url * @param int $page * @param string|null $rel * @return String */protected Functio N Getavailablepagewrapper ($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 ' &L T;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 so simple, the main thing is the render () method, if you need to modify the paging style in your project, or if you want to add a requirement like paging jump, just rewrite the HTML elements in the methods shown in the items, and it's flexible, and you need to fix that in the blade template, like our paginator called $ Users, the default paging display is this:

{!! $users->render ()!!}
modified to our custom pagination display:

{!! with (new \app\foundations\pagination\customerpresenter ($categories))->render ()!!}
OK, so on the page you should see the pagination link contains the "Previous" and "Next" plus the number of the style.

So if the extension simplepaginate? In fact very simple, as long as the inheritance just customerpresenter, realize haspages and render, as for why can I look at the source above the way to see it, such as we change 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 (
  Paginatorcontract $paginator) {$this->paginator = $paginator;
   }/** * Determine if the underlying paginator being presented 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 ()) {R Eturn New Htmlstring (sprintf (' <ul class= ' pager ">%s%s</ul> '), $this-&GT;getpreviousbutton (' previous '), $this->getnextbutton (' next article '));
  } return ';

 }

}

Pagination Display:

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

The method is to modify the corresponding display HTML elements according to their own needs.

Reprint Please specify: Reprinted from Ryan is rookie | LNMP Technology Stack Notes

The above is the entire content of this article, I hope that you learn PHP programming help.

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.