我正在尝试使用Zend Framework的2 Paginator来分割结果。但是,在导航到下一页链接时,我遇到了一个小问题。它给出了找不到页面的错误。我不确定到底发生了什么,所以我已经包括了我的路线,分页器路线和两个截图,以显示它发生了什么。
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,
),
),
从数据库中获取结果的方法:
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;
}
控制器:
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;
}
}
我认为这与路由有关,但我不确定所以我继续并包含了模型+控制器。如果需要,我可以包含视图,但我不认为它是必需的,因为它只是来自paginator对象的foreach循环。
正如你所看到的,路线是问题(我认为),但我不知道如何解决它。
感谢任何帮助!
由于
更新
我更改了分页路线,如下所示:
'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,
),
),
但这只是显示成员索引页面,其中包含网址localhost/members/lists-groups/page/2
(包含屏幕截图)。
再次获得任何帮助将非常感激,从我读过的Zend Paginator开始运行很简单,所以我不明白发生了什么......
更新2
这是我正在使用的分页控件。它在list-groups.phtml上调用
<?php echo $this->paginationControl($this->paginator, 'sliding', 'paginator.phtml', array('route' => 'members/lists-groups')); ?>
这是整个module.config.php文件
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',
)
),
);
答案 0 :(得分:1)
如您所知,您不需要 paginator 路线。 lists-groups 定义应该足够了。
我的假设是 lists-groups 实际上从未匹配,但在两种情况下都匹配另一条路线。
首先,将您的定义更改为:
'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
),
将优先级设置为您的路线。 删除所有其他路由,测试它是否有效,然后逐个添加它们以找到与之冲突的路由。
更新:页面参数是路线的一部分,所以要获得它:
$this->params()->fromRoute('page');