1

我是 ZF 的新手。我在我的第一个项目中使用了 zend 分页器。它工作正常,可以切换黑白页面并获得正确的结果,但问题是我在该视图中也有其他链接看看我的视图

<?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"; ?>

正如我所说的分页呈现和工作正常但是当我点击这些链接

 <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>

它没有调用所需的操作,而是使我的网址像这样

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

单击上述任何链接后,它就像这样

 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

请注意,当页面第一次呈现时它不会发生,但是当我点击任何分页链接时,它会首先执行所需的操作并显示视图......任何帮助这里是操作

 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;
     }
4

2 回答 2

2

尝试:

// 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> 

或者

<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> 

第二个示例使用baseUrl()使用前端控制器的 baseUrl 设置的视图助手。如果您没有在 frontController 中设置 baseUrl,它会尝试猜测。由于您没有使用引导功能来设置 baseUrl,您可以在index.php(非必需)中执行以下操作:

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

url()使用视图助手的第三种可能性:

<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> 
于 2012-01-13T17:01:54.103 回答
0

将此添加到您的操作中

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

并用这个替换你的链接

<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>
于 2012-01-17T07:50:20.157 回答