paginator生成的url在ZF2中不正确

时间:2013-09-01 12:53:30

标签: pagination zend-framework2

我遇到了ZF2 Paginating的一个问题。 paginator生成的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',
    ),
),

在IndexController中:

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-&gt; 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(
                        ),
                    ),
                ),

0 个答案:

没有答案