如何在zf2中获取表id?

时间:2014-04-18 09:06:21

标签: php zend-framework2 zend-controller

我在ZF2中创建了一个视图文件。我将id传递给控制器​​。

我如何在控制器中获得此ID?

这是我的showAction代码,我想获得id:

public function showAction()
{           
    $id = (int) $this->params()->fromRoute('id', 0);
    if (!$id) {
        return $this->redirect()->toRoute('calendar', array(
            'action' => 'create'
        ));
    }               
}

这是我的index.phtml,我将id传递给show controller:

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

1 个答案:

答案 0 :(得分:1)

您正在通过控制器中的此行路线获取您的ID

$id = (int) $this->params()->fromRoute('id', 0);

如果需要,您还可以通过以下行访问

$id = (int)$this->params('id');

如果您echo $id,则应获得id