詳解WordPress中添加友情鏈接的方法

字號:


    這篇文章主要介紹了PHP框架Laravel5.1插件Pagination實(shí)現(xiàn)自定義分頁的相關(guān)資料,需要的朋友可以參考下
    Laravel 的分頁很方便,其實(shí)擴(kuò)展起來也挺容易的,下面就來做個示例,擴(kuò)展一下 paginate() 和 simplePaginate() 方法,來實(shí)現(xiàn)我們自定義分頁樣式,比如顯示 "上一頁" 和 "下一頁" ,而不是 "《" 和 "》" ,當(dāng)然擴(kuò)展的方法掌握了你就可以肆無忌憚的擴(kuò)展一個你想要的分頁了,比如跳轉(zhuǎn)到某一頁,分頁顯示一共多少記錄,當(dāng)前顯示的記錄范圍等等巴拉巴拉的。。。
    5.1和5.2應(yīng)該是同樣的方法,我這里用的是5.2的版本。文檔告訴我們 Paginator 對應(yīng)于查詢語句構(gòu)造器和 Eloquent 的  simplePaginate 方法,而  LengthAwarePaginator 則等同于  paginate 方法。那我們還是來看下源碼,具體這個  paginate 是如何實(shí)現(xiàn) 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();
      }
    ......
    }
    render() 中傳入的是一個 Presenter 的實(shí)例,并調(diào)用這個實(shí)例化的 render 方法來實(shí)現(xiàn)分頁的顯示的。如果沒有則調(diào)用 BootstrapThreePresenter 中 render() 的,來看看 BootstrapThreePresenter 是干嘛的
    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 $window
       * @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 has 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()) {
          return new HtmlString(sprintf(
            '<ul>%s %s %s</ul>',
            $this->getPreviousButton(),
            $this->getLinks(),
            $this->getNextButton()
          ));
        }
        return '';
      }
    ......
    }
    這里可以看到 BootstrapThreePresenter 實(shí)現(xiàn)了 PresenterContract 的接口, render() 才是分頁顯示的真正實(shí)現(xiàn),構(gòu)造方法中的第一個參數(shù) PaginatorContract 其實(shí)就是一個 Paginator 我們繼續(xù)看下 PresenterContract 也就是 Presenter 接口中定義了什么方法需要實(shí)現(xiàn)
    illuminate/contracts/Pagination/Presenter.php
    <?php
    namespace Illuminate\Contracts\Pagination;
    interface Presenter 
    {
      /**
       * Render the given paginator.
       *
       * @return \Illuminate\Contracts\Support\Htmlable|string
       */
      public function render();
      /**
       * Determine if the underlying paginator being presented has pages to show.
       *
       * @return bool
       */
      public function hasPages();
    }
    其中定義了 render 和 hasPages 方法需要實(shí)現(xiàn)
    好了,那我們現(xiàn)在已經(jīng)很清晰了,我們要自定義分頁的顯示,那么就要寫一個我們自己的 Presenter 來實(shí)現(xiàn)接口中的 render() 和 hasPages() 就可以了。
    首先就來簡單的實(shí)現(xiàn)一個paginate(),顯示出來"上一頁"和"下一頁",中間是分頁數(shù)字的例子。
    新建文件如下(個人習(xí)慣)
    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 $window
       * @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 has 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()) {
          return new HtmlString(sprintf(
            '<ul>%s %s %s</ul>',
            $this->getPreviousButton('上一頁'),//具體實(shí)現(xiàn)可以查看該方法
            $this->getLinks(),
            $this->getNextButton('下一頁')//具體實(shí)現(xiàn)可以查看該方法
          ));
        }
        return '';
      }
      /**
       * 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 . '>' . $page . '</a></li>';
      }
      /**
       * Get HTML wrapper for disabled text.
       *
       * @param string $text
       * @return string
       */
      protected function getDisabledTextWrapper($text)
      {
        return '<li><span>' . $text . '</span></li>';
      }
      /**
       * Get HTML wrapper for active text.
       *
       * @param string $text
       * @return string
       */
      protected function getActivePageWrapper($text)
      {
        return '<li><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();
      }
    }
    就這么簡單,主要就是 render() 方法,如果項(xiàng)目中需要修改分頁樣式,或者添加分頁跳轉(zhuǎn)之類的需求只要重寫其中的各項(xiàng)顯示的方法中的html元素就可以了,很靈活,在blade模板中也需要修該,比如我們的 Paginator 叫 $users ,默認(rèn)的分頁顯示是這樣的:
    {!! $users->render() !!}
    修改成我們自定義后的分頁顯示:
    {!! with(new \App\Foundations\Pagination\CustomerPresenter($categories))->render() !!}
    好了,這樣在頁面應(yīng)該就可以看到分頁鏈接中含有 "上一頁"和"下一頁"加數(shù)字的樣式了。
    那么如果擴(kuò)展simplePaginate?其實(shí)很簡單,只要繼承剛才的 CustomerPresenter ,實(shí)現(xiàn) hasPages 和 render ,至于為什么可以按照我上面查看源碼的方式看一下就知道了,比如我們改成"上一篇"和"下一篇"
    新建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 has 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()) {
          return new HtmlString(sprintf(
            '<ul>%s %s</ul>',
            $this->getPreviousButton('上一篇'),
            $this->getNextButton('下一篇')
          ));
        }
        return '';
      }
    }
    分頁顯示:
    {!! with(new \App\Foundations\Pagination\CustomerSimplePresenter($categories))->render() !!}
    方法就是這個方法,具體修改按照自己需求重寫其中對應(yīng)的顯示html元素的方法就可以了。
    以上就是本文的全部內(nèi)容,希望對大家學(xué)習(xí)PHP程序設(shè)計(jì)有所幫助。