I'm trying to have a paginator split up the number of groups displayed on a page, but for some reason when I try to go to the next set of data or page, it defaults back to the main layout and index page. I'm not sure why this is because I have done this before and it worked fine.
Here is my code:
Route:
'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',
),
),
Controller -
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));
}
the view:
<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>
and the paginator view (not sure if I needed to include this but thought I should)
<?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 code
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);
}
),
);
}
}
I've included three screenshots (1 about how the paginator displays, and the second about how it redirects to the wrong page)
http://imgur.com/a/Cd269 - 1st
http://imgur.com/a/Yv3XL - 2nd
http://imgur.com/bOtDGYB - 3rd
I hope this is enough information, if not, please let me know and I will try my best to add more.
Thanks!
Update -
The route I am trying to get is localhost/members/group/view-more/page/2 and so forth but it redirects to localhost/members (the default layout) if next is clicked on and so forth in the paginator.
Also, here is the complete code for my controller (as requested)
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;
}
It looks like you have a type-o in your configuration. Depending on the logic in the rest of your controller it may decide to redirect or forward to another action, resulting in the incorrect page being shown (screenshot 3).
The name of your controller action is viewmore
but in the route configuration you have view-more
. This action can not be found. Either rename the action to viewMore
or change the configured action in your route to viewmore
:
Change the name of the action. The name of the action will be 'view-more' in configurations, routes etc.
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));
}
Change the name in the config so the action is correctly matched.
'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',
),
),
By the way, to make your life easier you could start referencing classes by their name like this: Members\Controller\Groups::class
. See this page about the class keyword. This way your editor will keep track of the classes usages.
The routes you created are called groups
and paginator
. In your code you're calling the route by members/groups
and members/paginator
. I believe that is incorrect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With