Analysis of Paging Implementation Method of Laravel Framework

  • 2021-10-16 01:18:58
  • OfStack

This paper describes the paging implementation method of Laravel framework with examples. Share it for your reference, as follows:

In the process of using Laravel, some functions "write dead" the expression of front-end pages, such as the page turning button of paging!

Of course, you will say that the Bootstrap style of Laravel is also very beautiful, but in actual projects, the page turning button often needs to meet the needs of customers, especially when developing an Web APP that supports mobile phone adaptation, it is necessary to use a custom style.

Therefore, learning a thing can't know a half solution, but study its principle.

First, let's take a look at how Laravel is paged, and where is the code for generating paging buttons?

Under Laravel Directory\ vendor\ laravel\ framework\ src\ Illuminate\ Pagination

Inheritance relations of classes under first principle 1

PresenterContract (parent class)
BootstrapThreePresenter (subclass) < -SimpleBootstrapThreePresenter
BootstrapFourPresenter (subclass) < -SimpleBootstrapFourPresenter

From the author's naming of classes, there must be differences, and we study them from the code

BootstrapThreePresenter. php and BootstrapFourPresenter. php differ mainly in the following functions

BootstrapThreePresenter. php code:


/**
* 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 '<li><a href="'.htmlentities($url).'" rel="external nofollow" rel="external nofollow" '.$rel.'>'.$page.'</a></li>';
}
/**
* Get HTML wrapper for disabled text.
*
* @param string $text
* @return string
*/
protected function getDisabledTextWrapper($text)
{
    return '<li class="disabled"><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>';
}

BootstrapFourPresenter. php code:


/**
* 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 '<li class="page-item"><a class="page-link" href="'.htmlentities($url).'" rel="external nofollow" rel="external nofollow" '.$rel.'>'.$page.'</a></li>';
}
/**
* Get HTML wrapper for disabled text.
*
* @param string $text
* @return string
*/
protected function getDisabledTextWrapper($text)
{
    return '<li class="page-item disabled"><a class="page-link">'.$text.'</a></li>';
}
/**
* Get HTML wrapper for active text.
*
* @param string $text
* @return string
*/
protected function getActivePageWrapper($text)
{
    return '<li class="page-item active"><a class="page-link">'.$text.'</a></li>';
}

We found that the biggest difference is that ThreePresenter is almost a "bare" HTML tag, while FourPresenter generates HTML tag with class.

Both ThreePresenter and FourPresenter have an render () function with the same implementation


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

Careful readers have noticed that there are two other inheritance classes, SimpleThreePresenter and SimpleFourPresenter. Since they are Simple (simple), the difference lies in their render () function


/**
* 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(),
        $this->getNextButton()
      ));
    }
    return '';
}

That is, the paging buttons generated by SimpleThreePresenter and SimpleFourPresenter have no "page number", only the "top page" and "bottom page" buttons.

More readers who are interested in Laravel can check the topics of this site: "Introduction and Advanced Tutorial of Laravel Framework", "Summary of Excellent Development Framework of php", "Introduction Tutorial of php Object-Oriented Programming", "Introduction Tutorial of php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"

I hope this article is helpful to the PHP programming based on Laravel framework.


Related articles: