当前位置:网站首页>Laravel customizes the paging style through the service provider

Laravel customizes the paging style through the service provider

2022-06-23 04:49:00 Mamba kids' shoe

Demand is introduced

Laravel Default paging , The implementation is very elegant , But sometimes you will encounter changing the default style , For example, I want to set the default <ul class="pagination"> It is amended as follows <ul class="pagination pagination-sm no-margin">

Solution entry point

Laravel The built-in paging link style is defined by Illuminate\Pagination\BootstrapThreePresenter Of render Method generation , We can make an article on this method .

Create override render Class of method

create a file :App/Presenters/PagiationPresenter

<?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 ''; } }

Create service provider PaginationServiceProvider

<?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() {
      // Custom paging  Paginator::presenter(function (AbstractPaginator $paginator) {
      return new PagiationPresenter($paginator); }); } /** * Register the application services. * * @return void */ public function register() {
      // } }

Add service provider to config/app.php

'providers' => [
        /*
         * Laravel Framework Service Providers...
         */
         ...
        App\Providers\PaginationServiceProvider::class,
    ],

Link to the original text :http://blog.kesixin.xin/article/52

原网站

版权声明
本文为[Mamba kids' shoe]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230004311016.html