Laravel框架分页实现方法分析
author:一佰互联 2019-04-25   click:163

本文实例讲述了Laravel框架分页实现方法。分享给大家供大家参考,具体如下:

Laravel使用的过程中,有些功能把前端页面的表达“写死了”,比如分页的翻页按钮!

当然你会说Laravel的Bootstrap样式也很好看啊,但是实际项目中,翻页按钮常常需要满足的客户的需要,特别在开发一款支持手机适配的Web APP,更是需要使用自定义的样式。

所以,学习一样东西不能一知半解,而是究其原理。

先来看看Laravel是怎么分页的,生成分页按钮的代码究竟写在了哪里?

Laravel目录vendorlaravelframeworksrcIlluminatePagination

先理一下类的继承关系

PresenterContract(父类)┗BootstrapThreePresenter(子类)<-SimpleBootstrapThreePresenterBootstrapFourPresenter(子类)<-SimpleBootstrapFourPresenter

从作者对类的命名上看,必有区别,我们从代码上研究

BootstrapThreePresenter.php和BootstrapFourPresenter.php主要区别在下列函数

BootstrapThreePresenter.php代码:

/*** 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代码:

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

我们发现最大的区别在ThreePresenter几乎是“裸”HTML标签,而FourPresenter生成的是带class的HTML标签。

无论是ThreePresenter还是FourPresenter,他们都有一个相同实现的render()函数

/*** Convert the URL window into Bootstrap HTML.** @return IlluminateSupportHtmlString*/public function render(){    if ($this->hasPages()) {      return new HtmlString(sprintf(        "<ul class="pagination">%s %s %s</ul>",        $this->getPreviousButton(),        $this->getLinks(),        $this->getNextButton()      ));    }    return "";}

细心的读者已经发觉,还有两个继承类,分别是SimpleThreePresenter和SimpleFourPresenter,既然是Simple(简单),区别就在他们的render()函数

/*** Convert the URL window into Bootstrap HTML.** @return IlluminateSupportHtmlString*/public function render(){    if ($this->hasPages()) {      return new HtmlString(sprintf(        "<ul class="pager">%s %s</ul>",        $this->getPreviousButton(),        $this->getNextButton()      ));    }    return "";}

也就是说,SimpleThreePresenter和SimpleFourPresenter生成的分页按钮是没有“页码”的,只有“上一页”和“下一页”按钮。

更多关于Laravel相关内容感兴趣的读者可查看本站专题:《Laravel框架入门与进阶教程》、《php优秀开发框架总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。