我遇到了 ZF2 分页的问题。分页器生成的 url 不正确。任何人都可以帮助指出我的配置中的错误位置吗?我只是在页面中做了一些小改动:http: //framework.zend.com/manual/2.2/en/modules/zend.paginator.usage.html
以下是我的更改:module.config.php:
'routes' => array(
'shop' => array(
'type' => 'Literal',
'options' => array(
'route' => '/shop',
'defaults' => array(
'__NAMESPACE__' => 'Shop\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'paginator' => array(
'type' => 'segment',
'options' => array(
'route' => '/index/list[/page/:page]',
'defaults' => array(
'page' => 1,
),
),
),
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action][/:id]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
'view_manager' => array(
....
'template_map' => array(
...
'index/page' => __DIR__ . '/../view/shop/index/page.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
在索引控制器中:
public function listAction(){
$array = array(
0 => array('id'=>1, 'name'=>'samsonasik'),
.......
);
$paginator = new \Zend\Paginator\Paginator(new \Zend\Paginator\Adapter\ArrayAdapter($array));
$paginator->setCurrentPageNumber($this->params()->fromRoute('page'));
$paginator->setItemCountPerPage(1);
$vm = new ViewModel();
$vm->setVariable('paginator', $paginator);
return $vm;
}
对于 list.phtml:
<?php echo $this->paginationControl($this->paginator,
'Sliding',
'index/page', array('route' => 'shop/paginator')); ?>
脚本类型 index/pgae 映射到 page.phtml。我在以下位置尝试了所有三个示例控件: http: //framework.zend.com/manual/2.2/en/modules/zend.paginator.usage.html#example-pagination-controls并更改了 $this->url() 的参数但都失败了。例如,对于搜索分页,我全部更改:
$this->url($this->route, array('page' => $this->previous));
至
$this->url($this->route, array('controller'=>'index','action'=>'list','page' => $page));
对于页面:HOST/shop/index/list,所有href的2,3..和分页控制中的下一个指向HOST/shop/index/list/page/2 HOST/shop/index/list/page/3这是我所期望的,但这个网址匹配 shop/default 而不是 shop/paginator
现在问题变得简单了:
如何编写 shop/paginator 路由器以便匹配 url HOST/shop/index/list/page/2:
'paginator' => array(
'type' => 'segment',
'options' => array(
'route' => '/index/list[/page/:page]',
'defaults' => array(
'page' => 1,
),
),
),
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action][/:id]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),