path方式的分页类 - Pager::Pathing()

最近的一个项目,用path方式表现页面的url:

http://www.foo.com/index.php/p/user:photo
http://www.foo.com/index.php/p/user:photo/page/1
http://www.foo.com/index.php/p/user:photo/page/2

正常的页面url:

http://www.foo.com/index.php?p=user:photo
http://www.foo.com/index.php?p=user:photo&page=1
http://www.foo.com/index.php?p=user:photo&page=2

之前使用的分页类是Pear:Pager,在这时候就玩不转了,不管如何调整参数分页链接总是错误,于是写了个Pathing.php,专门用于path方式的分页,调用方法:

PHP:
  1. $pager = Pager::factory(array('mode' => 'Pathing'));

Pager/Pathing.php

PHP:
  1. require_once('Pager/Jumping.php');
  2.  
  3. class Pager_Pathing extends Pager_Jumping {
  4.  
  5.     /**
  6.      * @see Pager_Common::_setOptions()
  7.      */
  8.     function _setOptions($options) {
  9.         $allowed_options = array(
  10.             'totalItems',
  11.             'perPage',
  12.             'delta',
  13.             'linkClass',
  14.             'path',
  15.             'fileName',
  16.             'append',
  17.             'httpMethod',
  18.             'importQuery',
  19.             'urlVar',
  20.             'altPrev',
  21.             'altNext',
  22.             'altPage',
  23.             'prevImg',
  24.             'nextImg',
  25.             'expanded',
  26.             'separator',
  27.             'spacesBeforeSeparator',
  28.             'spacesAfterSeparator',
  29.             'curPageLinkClassName',
  30.             'curPageSpanPre',
  31.             'curPageSpanPost',
  32.             'firstPagePre',
  33.             'firstPageText',
  34.             'firstPagePost',
  35.             'lastPagePre',
  36.             'lastPageText',
  37.             'lastPagePost',
  38.             'firstLinkTitle',
  39.             'nextLinkTitle',
  40.             'prevLinkTitle',
  41.             'lastLinkTitle',
  42.             'showAllText',
  43.             'itemData',
  44.             'clearIfVoid',
  45.             'useSessions',
  46.             'closeSession',
  47.             'sessionVar',
  48.             'pearErrorMode',
  49.             'extraVars',
  50.             'excludeVars',
  51.             'currentPage',
  52.         );
  53.  
  54.         foreach ($options as $key => $value) {
  55.             if (in_array($key, $allowed_options) && (!is_null($value))) {
  56.                 $this->{'_' . $key} = $value;
  57.             }
  58.         }
  59.  
  60.         $this->_fileName = ltrim($this->_fileName, '/')//strip leading slash
  61.         $this->_path     = rtrim($this->_path, '/');      //strip trailing slash
  62.  
  63.         if ($this->_append) {
  64.             $this->_fileName = CURRENT_FILENAME; //avoid possible user error;
  65.             $this->_url = $this->_path.'/'.$this->_fileName;
  66.         } else { //hacked for pathing pager
  67.    $this->_url = $this->_path;
  68.             if (strncasecmp($this->_fileName, 'javascript', 10) != 0)
  69.                 $this->_url .= '/';
  70.  
  71.    $len = strlen($this->_urlVar);
  72.    if((substr($this->_path, -$len, $len) == $this->_urlVar) and
  73.     is_numeric(CURRENT_FILENAME))
  74.     $this->_url = substr($this->_path, 0, -$len);
  75.    else
  76.     $this->_url.= CURRENT_FILENAME . '/';
  77.  
  78.             if (!strstr($this->_fileName, '%d'))
  79.                 trigger_error($this->errorMessage(ERROR_PAGER_INVALID_USAGE), E_USER_WARNING);
  80.         }
  81.  
  82.         $this->_classString = '';
  83.         if (strlen($this->_linkClass)) {
  84.             $this->_classString = 'class="'.$this->_linkClass.'"';
  85.         }
  86.  
  87.         if (strlen($this->_curPageLinkClassName)) {
  88.             $this->_curPageSpanPre  = '<span class="'.$this->_curPageLinkClassName.'">';
  89.             $this->_curPageSpanPost = '</span>';
  90.         }
  91.  
  92.         $this->_perPage = max($this->_perPage, 1); //avoid possible user errors
  93.  
  94.         if ($this->_useSessions && !isset($_SESSION)) {
  95.             session_start();
  96.         }
  97.         if (!empty($_REQUEST[$this->_sessionVar])) {
  98.             $this->_perPage = max(1, (int)$_REQUEST[$this->_sessionVar]);
  99.             if ($this->_useSessions) {
  100.                 $_SESSION[$this->_sessionVar] = $this->_perPage;
  101.             }
  102.         }
  103.  
  104.         if (!empty($_SESSION[$this->_sessionVar])) {
  105.              $this->_perPage = $_SESSION[$this->_sessionVar];
  106.         }
  107.  
  108.         if ($this->_closeSession) {
  109.             session_write_close();
  110.         }
  111.  
  112.         $this->_spacesBefore = str_repeat(' ', $this->_spacesBeforeSeparator);
  113.         $this->_spacesAfter  = str_repeat(' ', $this->_spacesAfterSeparator);
  114.  
  115.         $request = ($this->_httpMethod == 'POST') ? $_POST : $_GET;
  116.         if (isset($request[$this->_urlVar]) && empty($options['currentPage'])) {
  117.             $this->_currentPage = (int)$request[$this->_urlVar];
  118.         }
  119.         $this->_currentPage = max($this->_currentPage, 1);
  120.         $this->_linkData = $this->_getLinksData();
  121.  
  122.   return PAGER_OK;
  123.  }
  124.  
  125. }

调用例子:

PHP:
  1. require 'Pager/Pager.php';
  2.     $opt = array(
  3.         'mode'    => 'Pathing',       
  4.         'totalItems'    => $totalItems,
  5.         'perPage'      => $perPage,
  6.         'append'        => false,
  7.         'fileName'    => 'page/%d',
  8.         'urlVar'        => 'page',
  9.     );
  10.  
  11.     $pager = Pager::factory($opt);
  12.     $links = $pager->getLinks();

作者: volcano 发表于12月 31, 2005 at 1:33 pm

版权信息: 可以任意转载, 转载时请务必以超链接形式标明文章原始出处作者信息及此声明

Tags: ,,

留条评论