-1

ZF2.In でビュー ファイルを作成し、コントローラーに ID を渡します。

コントローラーでこのIDを取得するにはどうすればよいですか?

IDを取得したいshowActionコードは次のとおりです。

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

そして、これがコントローラーを表示するためにidを渡す私のindex.phtmlです:

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

1 に答える 1

1

コントローラーのこの行を介してルートからIDを取得しています

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

必要に応じて、次の行でもアクセスできます

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

あなたの場合echo $id、あなたはあなたのid価値を得る必要があります

于 2014-04-18T09:17:20.863 に答える