PHP¿ò¼ÜLaravel²å¼þPaginationʵÏÖ×Ô¶¨Òå·ÖÒ³

5年以前  |  阅读数:231 次  |  编程语言:PHP 

Laravel µÄÖÒ³ºÜ½±a£¬ÆaʵÀ(C)Õ¹ÆðÀ´Ò²Í¦ÈÝÒ׵ģ¬ÏÂÃae¾ÍÀ´×o¸oʾÀý£¬À(C)չһϠpaginate() ºÍ simplePaginate() ½¨£¬À´ÊµÏÖÎÒÃÇ×Ô¶¨ÒaÖÒ³Ñuʽ£¬±ÈÈçÏÔʾ "ÉÏÒ»Ò³" ºÍ "ÏÂÒ»Ò³" £¬¶ø²»ÊÇ "¡¶" ºÍ "¡" £¬µ±È»À(C)Õ¹µÄ½¨ÕÆÎÕÁËÄa¾Í¿ÉÒÔËÁÎ޼ɵ¬µÄÀ(C)Õ¹Ò»¸oÄaÏeÒªµÄÖÒ³ÁË£¬±ÈÈçÌøתµ½Ä³Ò»Ò³£¬ÖÒ³ÏÔʾһ¹²¶aÉټǼ£¬µ±Ç°ÏÔʾµÄ¼Ç¼*¶Î§µÈµÈ°ÍÀ­°ÍÀ­µÄ¡£¡£¡£

5.1ºÍ5.2Ó¦¸ÃÊÇͬÑuµÄ½¨£¬ÎÒÕaÀiÓõÄÊÇ5.2µÄ°ae±¾¡£Îĵµ¸aeËßÎÒÃÇ Paginator ¶ÔÓ¦ÓÚ²eѯÓi¾a¹¹ÔiÆ÷ºÍ Eloquent µÄ simplePaginate ½¨£¬¶ø LengthAwarePaginator ÔoµÈͬÓÚ paginate ½¨¡£ÄÇÎÒÃÇ»¹ÊÇÀ´¿´ÏÂÔ´Âe£¬¾ßÌaÕa¸o paginate ÊÇÈçºÎʵÏÖ 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() Öд«ÈeµÄÊÇÒ»¸o Presenter µÄʵÀý£¬²¢µ÷ÓÃÕa¸oʵÀý»¯µÄ render ½¨À´ÊµÏÖ*ÖÒ³µÄÏÔʾµÄ¡£Èç¹uûÓÐÔoµ÷Óà BootstrapThreePresenter ÖÐ render() µÄ£¬À´¿´¿´ BootstrapThreePresenter ÊǸÉÂiµÄ

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 class="pagination">%s %s %s</ul>',
            $this->getPreviousButton(),
            $this->getLinks(),
            $this->getNextButton()
          ));
        }

        return '';
      }
    ......
    }

ÕaÀi¿ÉÒÔ¿´µ½ BootstrapThreePresenter ʵÏÖÁË PresenterContract µÄ½Ó¿Ú£¬ render() ²ÅÊÇÖÒ³ÏÔʾµÄÕaeÕýʵÏÖ£¬¹¹Ôi½¨ÖеĵÚÒ»¸o²ÎÊý PaginatorContract Æaʵ¾ÍÊÇÒ»¸o Paginator ÎÒÃǼÌÐø¿´Ï PresenterContract Ò²¾ÍÊÇ Presenter ½Ó¿ÚÖж¨ÒaÁËʲô½*¨ÐeҪʵÏÖ

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();
    }

ÆaÖж¨ÒaÁË render ºÍ hasPages ½¨ÐeҪʵÏÖ

ºÃÁË£¬ÄÇÎÒÃÇÏÖÔÚÒѾ­ºÜÇaÎuÁË,ÎÒÃÇÒª×Ô¶¨Òa*ÖÒ³µÄÏÔʾ£¬ÄÇô¾ÍҪдһ¸oÎÒÃÇ×Ô¼ºµÄ Presenter À´ÊµÏÖ½Ó¿ÚÖÐµÄ render() ºÍ hasPages() ¾Í¿ÉÒÔÁË¡£

Ê×ÏȾÍÀ´¼oµ¥µÄʵÏÖÒ»¸opaginate()£¬ÏÔʾ³oÀ´"ÉÏÒ»Ò³"ºÍ"ÏÂÒ»Ò³"£¬ÖмaÊÇ*ÖÒ³Êý×ÖµÄÀý×Ó¡£

н¨ÎļþÈçÏ£¨¸oÈËÏ°¹ß£(C)

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 class="pagination">%s %s %s</ul>',
            $this->getPreviousButton('ÉÏÒ»Ò³'),//¾ßÌaʵÏÖ¿ÉÒÔ²e¿´¸Ã*½*¨
            $this->getLinks(),
            $this->getNextButton('ÏÂÒ»Ò³')//¾ßÌaʵÏÖ¿ÉÒÔ²e¿´¸Ã*½*¨
          ));
        }

        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 class="disabled hide"><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>';
      }

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

    }

¾ÍÕaô¼oµ¥£¬Ö÷Òª¾ÍÊÇ render() ½¨£¬Èç¹uÏiÄ¿ÖÐÐeÒªÐÞ¸ÄÖÒ³Ñuʽ£¬»oÕßÌi¼ÓÖÒ³Ìøת֮ÀaµÄÐeÇoÖ»ÒªÖØдÆaÖеĸ÷ÏiÏÔʾµÄ½¨ÖеÄhtmlÔªËؾͿÉÒÔÁË£¬ºÜÁe»i£¬ÔÚbladeÄ£°aÖÐÒ²ÐeÒªÐ޸㬱ÈÈçÎÒÃÇµÄ Paginator ½Ð $users ,ĬÈϵÄ*ÖÒ³ÏÔʾÊÇÕaÑuµÄ£º

{!! $users->render() !!}
Ð޸ijÉÎÒÃÇ×Ô¶¨ÒaºoµÄ*ÖÒ³ÏÔʾ£º

{!! with(new \App\Foundations\Pagination\CustomerPresenter($categories))->render() !!}
ºÃÁË£¬ÕaÑuÔÚÒ³ÃaeÓ¦¸Ã¾Í¿ÉÒÔ¿´µ½*ÖÒ³Á´½ÓÖк¬ÓÐ "ÉÏÒ»Ò³"ºÍ"ÏÂÒ»Ò³"¼ÓÊý×ÖµÄÑuʽÁË¡£

ÄÇôÈç¹uÀ(C)Õ¹simplePaginate?ÆaʵºÜ¼oµ¥£¬Ö»Òª¼Ì³Ð¸Õ²ÅµÄ CustomerPresenter £¬ÊµÏÖ hasPages ºÍ render ,ÖÁÓÚΪʲô¿ÉÒÔ°´ÕÕÎÒÉÏÃae²e¿´Ô´ÂeµÄ*½Ê½¿´Ò»Ï¾ÍÖªµÀÁË£¬±ÈÈçÎÒÃǸijÉ"ÉÏһƪ"ºÍ"ÏÂһƪ"

н¨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 class="pager">%s %s</ul>',
            $this->getPreviousButton('ÉÏһƪ'),
            $this->getNextButton('ÏÂһƪ')
          ));
        }

        return '';
      }

    }

*ÖÒ³ÏÔʾ£º

{!! with(new \App\Foundations\Pagination\CustomerSimplePresenter($categories))->render() !!}

½¨¾ÍÊÇÕa¸o½¨£¬¾ßÌaÐ޸İ´ÕÕ×Ô¼ºÐeÇoÖØдÆaÖжÔÓ¦µÄÏÔʾhtmlÔªËصĽ¨¾Í¿ÉÒÔÁË¡£

תÔØÇe×¢Ã÷£º×ªÔØ×Ô RyanÊDzËÄñ | LNMP¼¼ÊoÕ»±Ê¼Ç

ÒÔÉϾÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝ£¬Ï£Íu¶Ô´o¼ÒѧϰPHP³ÌÐoÉe¼ÆÓÐËu°iÖu¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
贪吃蛇
文件下载
双位运算符
发送邮件
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
提取后缀名
年的日历图
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例