laravel默認了分頁,實現很是優雅,但有時候會遇到修改默認的樣式,好比我要將默認的<ul class="pagination">
修改成<ul class="pagination pagination-sm no-margin">
php
Laravel自帶的分頁連接樣式由IlluminatePaginationBootstrapThreePresenter的render方法生成,咱們在此方法上作文章便可實現。laravel
建立文件:App/Presenters/PagiationPresentergit
<?php namespace App\Presenters; use Illuminate\Support\HtmlString; use Illuminate\Pagination\BootstrapThreePresenter; class PagiationPresenter extends BootstrapThreePresenter { public function render() { if ($this->hasPages()) { return new HtmlString(sprintf( '<ul class="pagination pagination-sm no-margin">%s %s %s</ul>', $this->getPreviousButton(), $this->getLinks(), $this->getNextButton() )); } return ''; } }
<?php namespace App\Providers; use App\Presenters\PagiationPresenter; use Illuminate\Pagination\Paginator; use Illuminate\Pagination\AbstractPaginator; use Illuminate\Support\ServiceProvider; class PaginationServiceProvider extends ServiceProvider { /** * Bootstrap the application services. * * @return void */ public function boot() { //自定義分頁 Paginator::presenter(function (AbstractPaginator $paginator) { return new PagiationPresenter($paginator); }); } /** * Register the application services. * * @return void */ public function register() { // } }
'providers' => [ /* * Laravel Framework Service Providers... */ ... App\Providers\PaginationServiceProvider::class, ],
QQ : 339803849 (歡迎加入)github
個人開源博客Moell Blogapp