我在ZF2中创建了一个视图文件。在该文件中,我将id传递给控制器。
如何在控制器中获取此id?
这是我想要获取id的showAction代码:
public function showAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('calendar', array(
'action' => 'create'
));
}
}
这是我的index.phtml,我在上面传递id来显示控制器:
<table class="table">
<tr>
<th>Calendar name</th>
<th>Description</th>
<th>Actions</th>
</tr>
<?php foreach ($calendars as $key => $value) : ?>
<tr>
<td>
<a href="<?php echo $this->url('calendar',array('action'=>'show', 'id' => $this->escapeHtml($value['_id'])));?>">
<?php echo $this->escapeHtml($value['title']);?>
</a>
</td>
<td><?php echo $this->escapeHtml($value['description']);?></td>
<td>
<a href="<?php echo $this->url('calendar',
array('action'=>'settings', 'id' => $this->escapeHtml($value['_id'])));?>">Settings</a>
<a href="<?php echo $this->url('calendar',
array('action'=>'delete', 'id' => $this->escapeHtml($value['_id'])));?>">delete</a>
</td>
</tr>
<?php endforeach; ?>
</table>
您正在通过控制器中的这条线路获取您的id
$id = (int) $this->params()->fromRoute('id', 0);
如果你想,你也可以通过下面的行访问它
$id = (int)$this->params('id');
如果是echo $id
,则应该获得id
值