phpzend-framework2zend-controller

how to get table id in zf2?


I make a view file in ZF2.In which i pass id to the controller.

How i get this id in controller?

Here is my showAction code where i want to get id:

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

and here is my index.phtml on which i pass id to 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>    

Solution

  • You are gettting your id fro route through this line in your controller

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

    If you want, you can also access it by the following line

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

    if you echo $id, you should get your id value