简体   繁体   中英

Zend Framework Paginator not displaying more than one page of results

I'm trying to use Zend Framework's 2 Paginator to split up results. However, I am running into a slight issue when navigating to the next page link. It is giving the error of page not found. I'm not sure exactly what is happening, so I've included my route, the paginator route and two screenshots to show what it is happening.

members/lists-groups route:

'lists-groups' => array(
    'type' => 'Segment',
    'options' => array(
        'route' => '/lists-groups/[page/:page]',
     ),

     'defaults' => array(
         'controller' => 'Members\Controller\ListsGroups',
         'action' => 'index',
     )
),

paginator route:

'paginator' => array(
    'type' => 'Segment',
    'options' => array(
         'route' => '/members/lists-groups/[page/:page]',
         'constraints' => array(
              'page'     => '[0-9]*',
         ),
     ),

     'defaults' => array(
         'controller' => 'Members\Controller\ListsGroups',
         'action'     => 'index',
         'page'       => 1,
     ),
 ), 

The method to fetch the results from the database:

public function browseAllGroups()
{
    $select = $this->select->from('groups');

    $result_set_prototype = new ResultSet();

    $result_set_prototype->setArrayObjectPrototype(new Groups());

    $paginator_adapter = new DbSelect($select, $this->gateway->getAdapter(), $result_set_prototype);

    $paginator = new Paginator($paginator_adapter);

    return $paginator;
}

the controller:

namespace Members\Controller;


use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;


class ListsGroupsController extends AbstractActionController
{
    protected $groups_service;


    public function indexAction()
    {
        $paginator = $this->getGroupsService()->browseAllGroups();

        $paginator->setCurrentPageNumber((int)$this->params()->fromQuery('page', 1));

        $paginator->setItemCountPerPage(5);

        return new ViewModel(array('paginator' => $paginator));
    }


    public function getGroupsService()
    {
        if (!$this->groups_service) {
            $this->groups_service = $this->getServiceLocator()->get('Members\Model\GroupsModel');
        }

        return $this->groups_service;
    }
}

I think it's something to do with the routing but am not sure so I went ahead and included the model + controller. I can include the view if needed but I didn't think it was needed as it's just a foreach loop from the paginator object.

The two screenshots: 在此处输入图片说明

在此处输入图片说明

As you can see, the routes are the issue (I think) but I have no idea on what to do to fix it.

Appreciate any help!

Thanks

Update:

I changed the pagination route so it is as follows:

'paginator' => array(
     'type' => 'Segment',
     'options' => array(
         'route' => 'lists-groups/[page/:page]',
         'constraints' => array(
              'page'     => '[0-9]*',
         ),
     ),

     'defaults' => array(
         'controller' => 'Members\Controller\ListsGroups',
         'action'     => 'index',
         'page'       => 1,
     ),
), 

but this just displays the members index page with the url localhost/members/lists-groups/page/2 (included a screenshot).

Any help again would be really appreciated, from what I've read Zend Paginator is simple to get running so I don't understand what is going on..

在此处输入图片说明

Update 2

This is the pagination control I am using. It is called on list-groups.phtml

<?php echo $this->paginationControl($this->paginator, 'sliding', 'paginator.phtml', array('route' => 'members/lists-groups')); ?>

Here is the entire module.config.php file

 return array(
    'controllers' => array(
        'invokables' => array(
            'Members\Controller\Members'        => 'Members\Controller\MembersController',
            'Members\Controller\Account'        => 'Members\Controller\AccountController',
            'Members\Controller\Messages'       => 'Members\Controller\MessagesController',
            'Members\Controller\Profile'        => 'Members\Controller\ProfileController',
            'Members\Controller\Groups'         => 'Members\Controller\GroupsController',
            'Members\Controller\Events'         => 'Members\Controller\EventsController',
            'Members\Controller\Status'         => 'Members\Controller\StatusController',
            'Members\Controller\Friends'        => 'Members\Controller\FriendsController',
            'Members\Controller\ListsGroups'    => 'Members\Controller\ListsGroupsController',
        ),
    ),


    'router' => array(
        'routes' => array(
            'members' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/members',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Members\Controller',
                        'controller'    => 'Members',
                        'action'        => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    '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_-]*',
                                'id'         => '[0-9]*',
                            ),
                            'defaults' => array(
                            ),
                        ),
                    ),

                    'status' => array(
                        'type'    => 'segment',
                        'options' => array(
                            'route' => '/status[/:action]',
                            'defaults' => array(
                                'controller' => 'Members\Controller\Status',
                                'action' => 'index',
                            ),
                        ),
                    ),

                    'edit-profile' => array(
                        'type'     => 'Segment',
                        'options'  => array(
                            'route' => '/edit-profile[/:action]',
                             'defaults' => array(
                                'controller' => 'Members\Controller\Profile',
                                'action'     => 'edit-profile',
                            ),
                        )
                    ),

                    'account' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route' => '/account[/:action]',
                            'defaults' => array(
                                'controller' => 'Members\Controller\Account',
                                'action'     => 'index',
                            ),
                        ),
                    ),

                    'messages' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route' => '/messages[/:action]',
                            'defaults' => array(
                                'controller' => 'Members\Controller\Messages',
                                'action'     => 'index',
                            ),
                        ),
                    ),

                    'profile' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route' => '/profile[/:action]',
                            'defaults' => array(
                                'controller' => 'Members\Controller\Profile',
                                'action'     => 'index',
                            ),
                        ),
                    ),

                    'friends' => array(
                        'type' => 'Segment',
                        'options' => array(
                            'route' => '/friends[/:action]',
                            'defaults' => array(
                                'controller' => 'Members\Controller\Friends',
                                'action' => 'index',
                            ),
                        ),
                    ),

                    'groups' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route' => '/groups[/:action][/:id]',
                            'constraints' => array(
                                'id'       => '[0-9]+',
                            ), 

                            'defaults' => array(
                                'controller' => 'Members\Controller\Groups',
                                'action'     => 'index',
                            ),
                        ),
                    ),

                    'events' => array(
                        'type'     => 'Segment',
                        'options'  => array(
                            'route' => '/events[/:action][/:id]',
                            'constraints' => array(
                                'id' => '[0-9]+',
                            ),

                            'defaults' => array(
                                'controller' => 'Members\Controller\Events',
                                'action'     => 'index',
                            ),
                        ),
                    ),

                    'group-admin' => array(
                        'type' => 'Segment',
                        'options' => array(
                            'route' => '/group-admin[/:action][/:id]',
                            'constraints' => array(
                                'id' => '[0-9]+',
                            ),
                        ),

                        'defaults' => array(
                            'controller' => 'Members\Controller\GroupAdmin',
                            'action'      => 'index',
                        ),
                    ),

                    'lists-groups' => array(
                        'type' => 'Segment',
                        'options' => array(
                            'route' => '/lists-groups[/page/:page]',

                            'defaults' => array(
                                'controller' => 'Members\Controller\ListsGroups',
                                'action' => 'index',
                            ),
                        ),
                    ),
                ),
            ),

            /*
            'paginator' => array(
                'type' => 'Segment',
                'options' => array(
                    'route' => 'lists-groups/[page/:page]',
                    'constraints' => array(
                        'page'     => '[0-9]*',
                    ),
                ),

                'defaults' => array(
                    'controller' => 'Members\Controller\ListsGroups',
                    'action'     => 'index',
                    'page'       => 1,
                ),
            ), */
        ),
    ),

    'form_elements' => array(
        'factories' => array(
            AddPhotosForm::class     => AddPhotosFormFactory::class,
            RemovePhotosForm::class  => RemovePhotosFormFactory::class,
            EditPhotosForm::class    => EditPhotosFormFactory::class,
        ),
    ),

    'view_manager' => array(
        'template_path_stack' => array(
            'Members' => __DIR__ . '/../view',
        ),

        'template_map' => array(
            'paginator' => __DIR__ . '/../view/layout/paginator.phtml',
        )
      ),
   );

As you figured out, you do not need the paginator route. The lists-groups definition should be enough.

My assumption is that the lists-groups is actually never matched, but another route is matched in both cases.

First of all, change your definition to:

'lists-groups' => array(
    'type' => 'Segment',
    'options' => array(
        'route' => '/lists-groups[/page/:page]',  // <--include the /
        'defaults' => array(                      // <-- defaults inside options
             'controller' => 'Members\Controller\ListsGroups',
             'action' => 'index',
        )
     ),
     'priority' => 100, // <-- priority
),

Set the priority to your route. Remove all other routes, test if it works and then add them one by one to find the one that there is a conflict with.

Update: The page param is part of the route, so to get it:

$this->params()->fromRoute('page'); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM