刚学Zend Framework 感叹其强大的同时却发现其在Web开发中常用到的如分页,上传,字符串处理等等却没有实现,今天小试牛刀,给Zend Framework扩展了一个分页类。
核心编写思路:
本分页类未采用常见的将HTML代码生成封装在类中的做法,而是基于Zend框架的MVC的思想,只在类中封装了处理分页所必需的数据的逻辑,而具体的分页信息显示逻辑则交予视图完成,将业务逻辑与显示逻辑分离,这样在开发灵活性上可以提高很多。
本例基于Zend Framework 1.5.2 编写,未将类整合到Zend Framework中,将本文件放置于INCLUDE_PATH/Util/下即可。本类只能在控制器下的方法中使用,实例化时需要传入控制器的request对象。
类核心代码:Page.php
- <?php
- /**
- * 基于Zend Framework分页类
- *
- * 作者:朦朧中的罪惡
- * 博客: http://be-evil.org
- *
- * @package Util
- */
- class Util_Page
- {
- /**
- * 数据总数
- * @var int
- */
- protected $_total;
- /**
- * 当前页码
- * @var int
- */
- protected $_curpage;
- /**
- * 每页记录数
- * @var int
- */
- protected $_perpage;
- /**
- * 分页参数名称
- * @var string
- */
- protected $_pagename;
- /**
- * Zend_Controller_Request_Http对象
- * @var object
- */
- protected $_request;
- /**
- * 类构造函数
- *
- * 初始化类内部属性
- *
- * @param int $total 总数
- * @param int $curpage 当前页码
- * @param int $perpage 每页记录数
- * @return void
- */
- public function __construct(Zend_Controller_Request_Http $request,$total,$perpage = 10,$pagename = 'page')
- {
- $this->_total = intval($total);
- $this->_perpage = 10;
- $this->_pagename = $pagename;
- $this->_request = $request;
- $this->_curpage = intval($this->_request->getParam($pagename));
- }
- /**
- *
- * 根据已经赋值的类属性计算相关参数
- *
- * @return array
- */
- public function calculatePage()
- {
- $pageArray = array();
- $pageArray['pagename'] = $this->_pagename;
- $pageArray['total'] = $this->_total;
- //算出总页数
- $pageArray['totalpage'] = (int) ceil($this->_total/$this->_perpage);
- $pageArray['pagestart'] = 0;
- //算出数据开始的行数
- if($this->_curpage < 1)
- {
- $this->_curpage = 1;
- }
- else
- {
- $this->_curpage > $pageArray['total'] && $this->_curpage = $pageArray['total'];
- $pageArray['pagestart'] = ($this->_curpage - 1) * $this->_perpage;
- }
- $pageArray['curpage'] = $this->_curpage;
- $pageArray['perpage'] = $this->_perpage;
- //通过传入的Zend_Controller_Request_Http类获得当前控制器的相关信息
- $moduleName = $this->_request->getModuleName();
- $controllerName = $this->_request->getControllerName();
- $actionName = $this->_request->getActionName();
- //获得参数
- $params = $this->_request->getParams();
- //初始化基本的链接
- $pageArray['url'] = '/'.$moduleName . '/' . $controllerName . '/' . $actionName . '/';
- if($params && is_array($params))
- {
- //反转数组的键与值
- $params = array_flip($params);
- //过滤参数中的页码参数和值
- $params = array_filter($params,array($this,'_filterPage'));
- //再次反转数组的键与值
- $params = array_flip($params);
- //循环生成参数链接
- foreach($params as $key => $value)
- {
- $pageArray['url'] .= $key . '/' . $value . '/';
- }
- }
- return $pageArray;
- }
- /**
- *
- * 清空类属性的赋值
- *
- * @return array
- */
- public function cleanUp()
- {
- $this->_curpage = $this->_perpage = $this->_curpage = 0;
- $this->_request = null;
- }
- /**
- * 设定内部属性$total的值
- *
- *
- * @return void
- */
- public function setTotal($total)
- {
- $this->_total = intval($total);
- }
- /**
- * 设定内部属性$curpage的值
- *
- *
- * @return void
- */
- public function setCurpage($curpage)
- {
- $this->_curpage = intval($curpage);
- }
- /**
- * 设定内部属性$perpage的值
- *
- *
- * @return void
- */
- public function setPerpage($perpage)
- {
- $this->_perpage = intval($perpage);
- }
- /**
- * 设定内部属性$perpage的值
- *
- *
- * @return void
- */
- public function setPagename($pagename)
- {
- $this->_pagename = $pagename;
- }
- /**
- * 设定内部对象$request
- *
- *
- * @return void
- */
- public function setRequest(Zend_Controller_Request_Http $request)
- {
- $this->_request = $request;
- }
- /**
- * 过滤Zend_Controller_Request_Http对象参数中的模块、控制器、动作及分页标识参数
- *
- *
- * @return bool
- */
- protected function _filterPage($paramname)
- {
- $filter = array('module','action','controller',$this->_pagename);
- return (in_array($paramname,$filter) ? false : true);
- }
- }
在控制器中的类的使用:
注意数据游标的偏移量已经在类中算好,调用数量则是分页类的每页条数属性
- public function indexAction()
- {
- //获得日志总数
- $blogcount = $this->_blog->getBlogCount();
- //实例化分类页对象
- $page = new Util_Page($this->_request,$blogcount);
- //计算相关数据
- $pages = $page->calculatePage();
- //根据分页算出的数据偏移量调用数据
- $blogs = $this->_blog->getBlogList($pages['perpage'],$pages['pagestart']);
- //视图变量赋值
- $this->view->assign(array('blogs'=>$blogs,'pages'=>$pages));
- }
在视图中的分页信息显示处理:
- <?php if($this->pages['totalpage'] > 1): ?>
- <Table width="98%" align="center">
- <tr>
- <td>
- 总<?php echo $this->pages['totalpage'] ?>页/<?php echo $this->pages['total'] ?>条记录 当前第<?php echo $this->pages['curpage'] ?>页
- <a href="<?php echo $this->pages['url'] ?>page/1/">首页</a>
- <?php if($this->pages['curpage'] > 1): ?>
- <a href="<?php echo $this->pages['url'] ?>page/<?php echo $this->pages['curpage']-1 ?>/">上一页</a>
- <?php endif; ?>
- |
- <?php if($this->pages['curpage'] <= $this->pages['totalpage']): ?>
- <a href="<?php echo $this->pages['url'] ?>page/<?php echo $this->pages['curpage']+1 ?>/">下一页</a>
- <?php endif; ?>
- <a href="<?php echo $this->pages['url'] ?>page/<?php echo $this->pages['totalpage'] ?>/">末页</a>
- </td>
- </tr>
- </Table>
- <?php endif; ?>
小结&心得
第一次用面向对象的思想写扩展,同时对Zend的机制了解还不是很透彻,可能类中的某些方法可以有更好的处理方式。在以后的学习中慢慢改进吧,最后依然对Zend没有封装网站常用业务逻辑感到费解....

