Php 类别中的Zf2分页(路由问题)

Php 类别中的Zf2分页(路由问题),php,pagination,zend-framework2,Php,Pagination,Zend Framework2,我正在尝试在我的新闻模块中创建分页 首先,我有以下几种模式: 所有新闻源 按类别提供 我需要为这两种模式创建分页。我刚刚为所有新闻提要创建了分页。没有问题。我有路径新闻+我添加了分页路径作为子路径: 'router' => array( 'routes' => array( 'news' => array( 'type' => 'Segment', 'options' => array(

我正在尝试在我的新闻模块中创建分页

首先,我有以下几种模式:

  • 所有新闻源
  • 按类别提供
  • 我需要为这两种模式创建分页。我刚刚为所有新闻提要创建了分页。没有问题。我有路径
    新闻
    +我添加了
    分页
    路径作为子路径:

    'router' => array(
        'routes' => array(
            'news' => array(
                'type' => 'Segment',
                'options' => array(
                    'route' => '/news',
                    'defaults' => array(
                        'controller' => 'News\Controller\Item',
                        'action'     => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'pagination' => array(
                        'type' => 'Segment',
                        'options' => array(
                            'route' => '/page-:page',
                            'constraints' => array(
                                'page'     => '[1-9][0-9]*',
                            ),
                            'defaults' => array(
                                'page'      => 1,
                            )
                        ),
                        'may_terminate' => true,
                    ),
                    // more child routes
                ),
            ),
        ),
    ),
    
    我使用的分页控件如下:

    <?=$this->paginationControl(
        $this->news,
        'Sliding',
        'pagination_control',
        array('route' => 'news/pagination')
    )?>
    
    <?=$this->paginationControl(
        $this->news,
        'Sliding',
        'pagination_control',
        array('route' => 'news/category/pagination', array('category' => $this->category->getUrl()))
    )?>
    
    嗯。现在我使用paginationControl,如下所示:

    <?=$this->paginationControl(
        $this->news,
        'Sliding',
        'pagination_control',
        array('route' => 'news/pagination')
    )?>
    
    <?=$this->paginationControl(
        $this->news,
        'Sliding',
        'pagination_control',
        array('route' => 'news/category/pagination', array('category' => $this->category->getUrl()))
    )?>
    
    
    
    现在,当我尝试使用分页控件访问任何页面时,我得到
    缺少参数“category”
    错误

    我可能理解出现此错误的原因:我指定了route
    news/category/pagination
    并指定了此route
    category
    的参数,但route
    news/category/pagination
    中没有参数
    category
    ,route
    新闻/category
    中有此参数,但我没有具体说明——这是错误的。我的假设正确吗

    那么,如何使分页与类别一起工作


    非常感谢

    最简单的方法就是将两条路线合并为一条

               'category-page' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '/:category/page-:page',
                        'constraints' => array(
                            'category'     => '[a-z]+',
                            'page'     => '[1-9][0-9]*'
                        ),
                        'defaults' => array(
                            'controller' => 'News\Controller\Item',
                            'action'     => 'category',
                            'page'      => 1
                        )
                    ),
                    'may_terminate' => true,
                ),
    
    你会成功的

    您还可以通过以下方式选择“页面”:

    'route' => '/:category[/page-:page]',
    

    这是最简单的方法。

    谢谢!如果无法使用我的路线结构,我将使用它。谢谢你的想法,但我已经使用了现有的路线:(有没有一种方法可以在不更改路由配置的情况下实现我想要的?关于路由的问题,文档中没有完整的解释。我打赌,当父级有参数时,您不能将分段路由用作另一分段路由的子级。您只能完成“路由->子路由1->子路由2”如果Route和ChildRoute1不是由参数组成的,比如/register/edit/:id、never/register/:edit/:id,您可以添加
    分页控件查看代码吗?作为新闻/类别的子级,新闻/类别/分页需要
    :category
    参数。您实际上不需要为其创建不同的路由pagination@Gabriel是斯坦dart.我不知道在哪里可以找到这个视图:(.
    作为news/category的子级,news/category/pagination需要:category参数
    –这就是问题:如何调用route
    news/category/pagination
    并设置父路由的参数
    news/category
    ?因此,您建议只在route中添加参数,而不创建新的子路由,正如前面所建议的那样?