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方式的分页,调用方法:

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

Pager/Pathing.php

require_once('Pager/Jumping.php');

class Pager_Pathing extends Pager_Jumping {

    /**
     * @see Pager_Common::_setOptions()
     */
    function _setOptions($options) {
        $allowed_options = array(
            'totalItems',
            'perPage',
            'delta',
            'linkClass',
            'path',
            'fileName',
            'append',
            'httpMethod',
            'importQuery',
            'urlVar',
            'altPrev',
            'altNext',
            'altPage',
            'prevImg',
            'nextImg',
            'expanded',
            'separator',
            'spacesBeforeSeparator',
            'spacesAfterSeparator',
            'curPageLinkClassName',
            'curPageSpanPre',
            'curPageSpanPost',
            'firstPagePre',
            'firstPageText',
            'firstPagePost',
            'lastPagePre',
            'lastPageText',
            'lastPagePost',
            'firstLinkTitle',
            'nextLinkTitle',
            'prevLinkTitle',
            'lastLinkTitle',
            'showAllText',
            'itemData',
            'clearIfVoid',
            'useSessions',
            'closeSession',
            'sessionVar',
            'pearErrorMode',
            'extraVars',
            'excludeVars',
            'currentPage',
        );

        foreach ($options as $key => $value) {
            if (in_array($key, $allowed_options) && (!is_null($value))) {
                $this->{'_' . $key} = $value;
            }
        }

        $this->_fileName = ltrim($this->_fileName, '/');  //strip leading slash
        $this->_path     = rtrim($this->_path, '/');      //strip trailing slash

        if ($this->_append) {
            $this->_fileName = CURRENT_FILENAME; //avoid possible user error;
            $this->_url = $this->_path.'/'.$this->_fileName;
        } else { //hacked for pathing pager
   $this->_url = $this->_path;
            if (strncasecmp($this->_fileName, 'javascript', 10) != 0)
                $this->_url .= '/';

   $len = strlen($this->_urlVar);
   if((substr($this->_path, -$len, $len) == $this->_urlVar) and
    is_numeric(CURRENT_FILENAME))
    $this->_url = substr($this->_path, 0, -$len);
   else
    $this->_url.= CURRENT_FILENAME . '/';

            if (!strstr($this->_fileName, '%d'))
                trigger_error($this->errorMessage(ERROR_PAGER_INVALID_USAGE), E_USER_WARNING);
        }

        $this->_classString = '';
        if (strlen($this->_linkClass)) {
            $this->_classString = 'class="'.$this->_linkClass.'"';
        }

        if (strlen($this->_curPageLinkClassName)) {
            $this->_curPageSpanPre  = '';
            $this->_curPageSpanPost = '';
        }

        $this->_perPage = max($this->_perPage, 1); //avoid possible user errors

        if ($this->_useSessions && !isset($_SESSION)) {
            session_start();
        }
        if (!empty($_REQUEST[$this->_sessionVar])) {
            $this->_perPage = max(1, (int)$_REQUEST[$this->_sessionVar]);
            if ($this->_useSessions) {
                $_SESSION[$this->_sessionVar] = $this->_perPage;
            }
        }

        if (!empty($_SESSION[$this->_sessionVar])) {
             $this->_perPage = $_SESSION[$this->_sessionVar];
        }

        if ($this->_closeSession) {
            session_write_close();
        }

        $this->_spacesBefore = str_repeat(' ', $this->_spacesBeforeSeparator);
        $this->_spacesAfter  = str_repeat(' ', $this->_spacesAfterSeparator);

        $request = ($this->_httpMethod == 'POST') ? $_POST : $_GET;
        if (isset($request[$this->_urlVar]) && empty($options['currentPage'])) {
            $this->_currentPage = (int)$request[$this->_urlVar];
        }
        $this->_currentPage = max($this->_currentPage, 1);
        $this->_linkData = $this->_getLinksData();

  return PAGER_OK;
 }
 
}

调用例子:


	require 'Pager/Pager.php';
	$opt = array(
		'mode' 			=> 'Pathing',		
		'totalItems'	=> $totalItems,
		'perPage'		=> $perPage,
		'append'		=> false,
		'fileName'		=> 'page/%d',
		'urlVar'		=> 'page',
	);

	$pager = Pager::factory($opt);
	$links = $pager->getLinks();

Leave a Reply

Your email address will not be published.