我试图让分页器分割页面上显示的组数,但出于某种原因,当我尝试转到下一组数据或页面时,它会默认返回主布局,索引页面。我不知道为什么会这样,因为我之前做过这件事并且工作正常。
这是我的代码:
路线:
'groups' => array(
'type' => 'Segment',
'options' => array(
'route' => '/groups[/:action][/:id]',
'constraints' => array(
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Members\Controller\Groups',
'action' => 'index',
),
),
),
'paginator' => array(
'type' => 'Segment',
'options' => array(
'route' => '/groups/view-more/[page/:page]',
'constraints' => array(
'page' => '[0-9]*',
),
),
'defaults' => array(
'controller' => 'Members\Controller\Groups',
'action' => 'view-more',
),
),
控制器 -
public function viewmoreAction()
{
$paginator = new Paginator(new DbTableGateway($this->getGroupsTable()));
$page = 1;
if ($this->params()->fromRoute('page')) {
$page = $this->params()->fromRoute('page');
}
$paginator->setCurrentPageNumber((int)$page);
$paginator->setItemCountPerPage(5);
return new ViewModel(array('paginator' => $paginator));
}
观点:
<div class="w3-row">
<div class="w3-col sm-12 w3-center">
<?php if (count($this->paginator) <= 0): ?>
<p class="w3-center">No more groups found</p>
<?php else: ?>
<div class="w3-responsive">
<table class="w3-table-all w3-card-4">
<thead>
<tr class="w3-white">
<th style="white-space: nowrap;">Group Id</th>
<th style="white-space: nowrap;">Group Name</th>
</tr>
</thead>
<?php
foreach ($this->paginator as $rows):
?>
<tr class="w3-hover-text-red w3-text-black">
<td><a href="<?php echo $this->url('members/groups', array('action' => 'group-home', 'id' => $rows['group_id'])); ?>">
<?php echo $rows['group_id']; ?>
</a></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
<br><br>
<div class="w3-right">
<?php echo $this->paginationControl($this->pagination, 'Sliding', 'pagination.phtml', array('route' => 'members/paginator')); ?>
</div>
</div>
</div>
和paginator视图(不确定我是否需要包含此内容,但我认为应该这样做)
<?php if ($this->pageCount): ?>
<div class="w3-bar">
<!-- 1st page link -->
<?php echo $this->firstItemNumber; ?> - <?php echo $this->lastItemNumber; ?> of <?php echo $this->totalItemCount; ?>
<?php if (isset($this->previous)): ?>
<a href="<?php echo $this->url($this->route, array('page' => $this->first)); ?>" class="w3-button">First</a> |
<?php else: ?>
<span class="w3-button w3-disabled">First</span> |
<?php endif; ?>
<!-- previous page link -->
<?php if (isset($this->previos)): ?>
<a href="<?php echo $this->url($this->route, array('page' => $this->previous)); ?>" class="w3-button">< Previous</a> |
<?php else: ?>
<span class="w3-button w3-disabled">Previous</span> |
<?php endif; ?>
<!-- next page link -->
<?php if (isset($this->next)): ?>
<a href="<?php echo $this->url($this->route, array('page' => $this->next)); ?>" class="w3-button">Next ></a> |
<?php else: ?>
<span class="w3-button w3-disabled">Next ></span> |
<?php endif; ?>
<!-- last page link -->
<?php if (isset($this->next)): ?>
<a href="<?php echo $this->url($this->route, array('page' => $this->last)); ?>" class="w3-button">Last</a>
<?php else: ?>
<span class="w3-button w3-disabled">Last</span>
<?php endif; ?>
</div>
Module.php代码
class Module implements AutoloaderProviderInterface
{
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . str_replace('\\', '/' , __NAMESPACE__),
),
),
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$eventManager->attach(MvcEvent::EVENT_ROUTE, array($this, 'checkCredentials'));
$eventManager->attach(MvcEvent::EVENT_ROUTE, array($this, 'configureLayout'));
}
public function checkCredentials(MvcEvent $e)
{
$matches = $e->getRouteMatch();
if (!$matches) {
return $e;
}
$route = $matches->getMatchedRouteName();
if (0 !== strpos($route, 'members/') && $route !== 'members') {
return $e;
}
$auth_service = $e->getApplication()->getServiceManager()->get('pblah-auth');
if (!$auth_service->hasIdentity()) {
$response = $e->getResponse();
$response->setStatusCode(302);
$response->getHeaders()
->addHeaderLine('Location', $e->getRouter()->assemble([], array('name' => 'home/member-login')));
$response->sendHeaders();
return $response;
}
return $e;
}
public function configureLayout(MvcEvent $e)
{
if ($e->getError()) {
return $e;
}
$request = $e->getRequest();
if (!$request instanceof Http\Request || $request->isXmlHttpRequest()) {
return $e;
}
$matches = $e->getRouteMatch();
if (!$matches) {
return $e;
}
$app = $e->getParam('application');
$layout = $app->getMvcEvent()->getViewModel();
$controller = $matches->getParam('controller');
$module = strtolower(explode('\\', $controller)[0]);
if ('members' === $module) {
$layout->setTemplate('layout/members');
}
}
public function getServiceConfig()
{
return array(
'factories' => array(
'Members\Module\EditProfileModel' => function ($sm) {
$table_gateway = $sm->get('EditProfileService');
$profile = new EditProfileModel($table_gateway);
return $profile;
},
'EditProfileService' => function ($sm) {
$db_adapter = $sm->get('Zend\Db\Adapter\Adapter');
$result_set_prototype = new ResultSet();
$result_set_prototype->setArrayObjectPrototype(new EditProfile());
return new TableGateway('profiles', $db_adapter, null, $result_set_prototype);
},
'Members\Model\ProfileModel' => function ($sm) {
$table_gateway = $sm->get('ProfileService');
$profile = new ProfileModel($table_gateway, $sm->get('pblah-auth')->getIdentity());
return $profile;
},
'ProfileService' => function ($sm) {
$db_adapter = $sm->get('Zend\Db\Adapter\Adapter');
return new TableGateway('profiles', $db_adapter);
},
'Members\Model\GroupsModel' => function ($sm) {
$table_gateway = $sm->get('GroupsService');
$group_model = new GroupsModel($table_gateway, $sm->get('pblah-auth')->getIdentity());
return $group_model;
},
'GroupsService' => function ($sm) {
$db_adapter = $sm->get('Zend\Db\Adapter\Adapter');
return new TableGateway('groups', $db_adapter);
}
),
);
}
}
我已经包含了三个屏幕截图(1个关于如何显示分页器,第二个关于如何重定向到错误的页面)
http://imgur.com/a/Cd269 - 第1次
http://imgur.com/a/Yv3XL - 第二次
http://imgur.com/bOtDGYB - 第3次
我希望这是足够的信息,如果没有,请告诉我,我会尽力添加更多信息。
谢谢!
更新 -
我想要获取的路由是localhost / members / group / view-more / page / 2等等,但是如果在下一个点击中等等,它会重定向到localhost / members(默认布局)等等
此外,这是我的控制器的完整代码(根据要求)
class GroupsController extends AbstractActionController
{
protected $groups_service;
protected $groups_table;
public function indexAction()
{
return new ViewModel(array('groups' => $this->getGroupsService()->listGroupsIndex()));
}
public function viewallaction()
{
return new ViewModel(array('groups' => $this->getGroupsService()->getAllUserGroups()));
}
public function viewmoreAction()
{
$paginator = new Paginator(new DbTableGateway($this->getGroupsTable(), array('member_id' => $this->getGroupsService()->grabUserId())));
$page = 1;
if ($this->params()->fromRoute('page')) {
$page = $this->params()->fromRoute('page');
}
$paginator->setCurrentPageNumber((int)$page);
$paginator->setItemCountPerPage(5);
return new ViewModel(array('paginator' => $paginator));
}
public function getgroupsAction()
{
$layout = $this->layout();
$layout->setTerminal(true);
$view_model = new ViewModel();
$view_model->setTerminal(true);
echo json_encode($this->getGroupsService()->listGroups());
return $view_model;
}
public function getgroupmembersonlineAction()
{
$layout = $this->layout();
$layout->setTerminal(true);
$view_model = new ViewModel();
$view_model->setTerminal(true);
try {
echo json_encode($this->getGroupsService()->getGroupMemsOnline());
} catch (GroupMembersOnlineException $e) {
echo json_encode($e->getMessage());
}
return $view_model;
}
public function grouphomeAction()
{
$id = $this->params()->fromRoute('id', 0);
if (0 === $id) {
return $this->redirect()->toRoute('members/groups', array('action' => 'index'));
}
if (!$this->getGroupsService()->getGroupInformation($id)) {
return $this->redirect()->toRoute('members/groups', array('action' => 'index'));
}
return new ViewModel(array('group_info' => $this->getGroupsService()->getGroupInformation($id)));
}
public function getonegroupmembersonlineAction()
{
$layout = $this->layout();
$layout->setTerminal(true);
$view_model = new ViewModel();
$view_model->setTerminal(true);
$id = $this->params()->fromRoute('id');
try {
echo json_encode($this->getGroupsService()->getGroupMemsOnline($id));
} catch (GroupMembersOnlineException $e) {
echo json_encode($e->getMessage());
}
return $view_model;
}
public function leavegroupAction()
{
$layout = $this->layout();
$layout->setTerminal(true);
$view_model = new ViewModel();
$view_model->setTerminal(true);
$group_id = $this->params()->fromRoute('id');
try {
echo json_encode($this->getGroupsService()->leaveTheGroup($group_id));
} catch (GroupsException $e) {
echo json_encode($e->getMessage());
}
return $view_model;
}
public function creategroupAction()
{
$form = new CreateGroupForm();
return new ViewModel(array(
'form' => $form
));
}
public function cgroupAction()
{
$form = new CreateGroupForm();
$request = $this->getRequest();
if ($request->isPost()) {
$create_group = new CreateGroup();
$form->setInputFilter($create_group->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$create_group->exchangeArray($form->getData());
try {
if ($this->getGroupsService()->createNewGroup($create_group)) {
$this->flashMessenger()->addSuccessMessage("Group was created successfully!");
return $this->redirect()->toUrl('create-group-success');
}
} catch (GroupsException $e) {
$this->flashMessenger()->addErrorMessage((string)$e->getMessage());
return $this->redirect()->toUrl('create-group-failure');
}
} else {
$this->flashMessenger()->addErrorMessage("Invalid form. Please correct this and try again.");
return $this->redirect()->toUrl('create-group-failure');
}
}
}
public function postgroupmessageAction()
{
}
public function postgroupeventAction()
{
}
public function joingroupAction()
{
$id = $this->params()->fromRoute('id');
$form = new JoinGroupForm();
return new ViewModel(array('form' => $form, 'id' => $id));
}
public function jgroupAction()
{
$form = new JoinGroupForm();
$request = $this->getRequest();
if ($request->isPost()) {
$join_group = new JoinGroup();
$form->setInputFilter($join_group->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$join_group->exchangeArray($form->getData());
try {
if (false !== $this->getGroupsService()->joinTheGroup($_POST['group_id'], $join_group)) {
$this->flashMessenger()->addSuccessMessage("Request to join group sent.");
return $this->redirect()->toUrl('join-group-success');
}
} catch (GroupsException $e) {
$this->flashMessenger()->addErrorMessage((string)$e->getMessage());
return $this->redirect()->toUrl('join-group-failure');
}
} else {
$messages = $form->getMessages();
$this->flashMessenger()->addErrorMessage("Invalid form. Please correct this and try again.");
return $this->redirect()->toUrl('join-group-failure');
}
}
}
public function joingroupsuccessAction()
{
}
public function joingroupfailureAction()
{
}
public function viewgroupsAction()
{
return new ViewModel(array('groups' => $this->getGroupsService()->listAllGroups()));
}
public function creategroupsuccessAction()
{
}
public function creategroupfailureAction()
{
}
public function getGroupsService()
{
if (!$this->groups_service) {
$this->groups_service = $this->getServiceLocator()->get('Members\Model\GroupsModel');
}
return $this->groups_service;
}
public function getGroupsTable()
{
if (!$this->groups_table) {
$this->groups_table = new TableGateway('group_members', $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter'));
}
return $this->groups_table;
}
答案 0 :(得分:7)
您的配置中似乎有一个类型o。根据控制器其余部分的逻辑,它可能决定重定向或转发到另一个操作,导致显示错误的页面(屏幕截图3)。
您的控制器操作的名称为viewmore
,但在路由配置中您有view-more
。找不到此操作。将操作重命名为viewMore
或将路线中已配置的操作更改为viewmore
:
更改操作的名称。操作的名称将在配置,路线等中“查看更多”。
public function viewMoreAction()
{
$paginator = new Paginator(new DbTableGateway($this->getGroupsTable()));
$page = 1;
if ($this->params()->fromRoute('page')) {
$page = $this->params()->fromRoute('page');
}
$paginator->setCurrentPageNumber((int)$page);
$paginator->setItemCountPerPage(5);
return new ViewModel(array('paginator' => $paginator));
}
更改配置中的名称,以便正确匹配操作。
'paginator' => array(
'type' => 'Segment',
'options' => array(
'route' => '/groups/view-more/[page/:page]',
'constraints' => array(
'page' => '[0-9]*',
),
),
'defaults' => array(
'controller' => 'Members\Controller\Groups',
'action' => 'viewmore',
),
),
顺便说一下,为了让您的生活更轻松,您可以按照以下名称开始引用类:Members\Controller\Groups::class
。有关class关键字的信息,请参阅this page。这样,您的编辑器将跟踪类的使用情况。
您创建的路线名为groups
和paginator
。在您的代码中,您通过members/groups
和members/paginator
呼叫路由。我认为这是不正确的。