zend paginator make other links and Action Calling concatinating with the URL

508 views Asked by At

I am NEW to ZF .i used zend paginator in my first project .its working fine that is switching b/w pages with right result but the problem is that i have other links too in that view have a look to my view

<?php include "header.phtml"; ?>
<h1><?php echo  $this->escape($this->title);?></h1>
<h2><?php echo $this->escape($this->description);?></h2>
<a href="register">Register</a>
<table border="1" align="center">
<tr>
  <th>User Name</th>      
  <th>First Name</th>     
  <th>Last Name</th>      
  <th>Action</th>     
</tr>
 <?php 
 foreach($this->paginator as $record){?>  
<tr>
  <td><?php echo $record->user_name;?></td>
  <td><?php echo $record->first_name;?></td>      
  <td><?php echo $record->last_name;?></td>   
  <td>
    <a  href="edit/id/<?php echo $record->id;?>">Edit</a>
    |
    <a  href="del/id/<?php echo $record->id;?>">Delete</a>      
  </td>   
</tr> 
  <?php } ?>
 </table>

<?php echo $this->paginationControl($this->paginator, 'Sliding', 'pagination.phtml'); ?>

 <?php include "footer.phtml"; ?>

as i said the pagination renders and working fine but when i click on these links

 <a  id="edit_link" href="edit/id/<?php echo $record->id;?>">Edit</a>
                      or
 <a id="delete_link" href="del/id/<?php echo $record->id;?>">Delete</a>
                      or
 <a href="register">Register</a>

it is not calling the required action instead it make my url like this

(initial link) http://localhost/zend_login/web_root/index.php/task/list

after clicking any of the above link its like this

 http://localhost/zend_login/web_root/index.php/task/list/page/edit/id/8
 http://localhost/zend_login/web_root/index.php/task/list/page/edit/id/edit/id/23
 http://localhost/zend_login/web_root/index.php/task/list/page/edit/id/edit/id/register        http://localhost/zend_login/web_root/index.php/task/list/page/edit/id/edit/id/del/id/12

note its not happening when the page renders first time but when i click on any pagination link its doing so initialy its going to the reguired action and displaying a view...any help HERE IS THE ACTION

 public function listAction(){

      $registry = Zend_Registry::getInstance();  
      $DB = $registry['DB'];
      $sql = "SELECT * FROM task ORDER BY task_name ASC";
      $result = $DB->fetchAll($sql);
      $page=$this->_getParam('page',1);
      $paginator = Zend_Paginator::factory($result);
      $paginator->setItemCountPerPage(3);
      $paginator->setCurrentPageNumber($page);
      $this->view->assign('title','Task List');
      $this->view->assign('description','Below, are the  Task:');
      $this->view->paginator=$paginator;
     }
2

There are 2 answers

0
Fawad Ghafoor On BEST ANSWER

add this in your action

$request = $this->getRequest();
$this->view->assign('url', $request->getBaseURL());

and replace your links in view with this

<a href="<?php echo $this->url."/task/register"?>">Add a Task</a>
<a href="<?php echo $this->url.'/task/edit/id/'.$record->id;?>">Edit</a>
<a href="<?php echo $this->url.'/task/del/id/'.$record->id;?>">Delete</a>
0
BartekR On

Try:

// controller
$this->view->controllerName = $this->getRequest()->getControllerName();

// view script
<a href="<?php echo $this->controllerName . '/edit/id/' . $record->id);?>">Edit</a> 
| 
<a href="<?php echo $this->controllerName . '/del/id/' . $record->id);?>">Delete</a> 

or

<a href="<?php echo $this->baseUrl($controllerName . '/edit/id/' . $record->id);?>">Edit</a> 
| 
<a href="<?php echo $this->baseUrl($controllerName . '/del/id/' . $record->id);?>">Delete</a> 

Second example uses baseUrl() view helper that's using front controller's baseUrl setting. If you don't set baseUrl in your frontController it's trying to guess. As you're not using bootstrap functionality to set baseUrl you may do the following in index.php (not required):

$frontController = Zend_Controller_Front::getInstance(); 
$frontController->setBaseUrl('/');

Third possibility using url() view helper:

<a href="<?php echo $this->url(array(
    'controller' => $controllerName,
    'action'     => 'edit',
    'id'         => $record_->id
)); ?>">Edit</a> 
| 
<a href="<?php echo $this->url(array(
    'controller' => $controllerName,
    'action'     => 'del',
    'id'         => $record_->id
));?>">Delete</a>