我是ZF的新手。我在我的第一个项目中使用了zend paginator。工作正常,正在切换b / w页面,但是问题是我在该视图中也有其他链接查看我的视图
<?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
注意当页面呈现第一次时没有发生,但是当我点击任何分页链接时,它会首先执行所需的操作并显示视图...任何帮助这里是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;
}
答案 0 :(得分: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>
答案 1 :(得分: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>