【发布时间】:2018-02-14 16:13:17
【问题描述】:
我正在尝试使用 Zend Framework 的 2 Paginator 来拆分结果。但是,导航到下一页链接时遇到了一个小问题。它给出了找不到页面的错误。我不确定到底发生了什么,所以我已经包含了我的路线、分页器路线和两个屏幕截图来显示它正在发生的事情。
成员/列表组路由:
'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' => '/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;
}
}
我认为这与路由有关,但不确定,所以我继续并包括模型 + 控制器。如果需要,我可以包含视图,但我认为不需要它,因为它只是来自分页器对象的 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,
),
),
但这只是显示带有 url 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',
)
),
);
【问题讨论】:
-
您的成员路由名称是否像您在分页器配置中提到的那样以斜杠开头?我认为这是路由数组的关键,而不是目标 url。
-
你到底是什么意思?这是成员路由的子路由
-
list-groups是路由名称(成员的子路由,因此它是成员/列表组) -
İts 以反斜杠开头:{'route' => '/members/lists-groups/[page/:page]',}
-
如果我去掉反斜杠,页面会重定向到 localhost/members/members/lists-groups/page/2
标签: php pagination zend-framework2