1

在我的 ContactsController 中,我添加了方法/操作,如果成功添加了联系人,我会设置一条消息。并将其重定向到 index.ctp

public function add() {
        if ($this->request->is('POST')) {
            $this->Contact->create();
            if ($this->Contact->save($this->request->data)) {
                $this->Session->setFlash('Contact added successfully!');
                $this->redirect(array('action'=>'index')); 
            } else {
                $this->Session->setFlash('Sorry! Contact is not added.');
            }
        }
    }

当我成功添加联系人时,会显示消息。但是当我去添加视图时,会显示 Flash 消息。请帮忙!

<?php echo $this->Session->flash(); ?>
    <h3>Address Book</h3>
    <table class="table table-striped">
        <tr>
            <th>Name</th>
            <th>Email</th>
            <th>Address</th>
            <th>City</th>
            <th>State</th>
            <th>Zip Code</th>
        </tr>
        <?php foreach ($contacts as $contact): ?>
        <tr>
            <td>
            <?php 
            /*echo $this->Html->link($contact['Contact']['firstname']." "
                .$contact['Contact']['lastname'], 
                array('controller' => 'contacts', 'action' => 'view',
                    $contact['Contact']['id'])); */
            echo $this->Html->link($contact['Contact']['firstname']." "
                .$contact['Contact']['lastname'],
                array('action' => 'view', $contact['Contact']['id']));
            ?>
            </td>
            <td><?php echo $contact['Contact']['email']; ?></td>
            <td><?php echo $contact['Contact']['address1']; ?></td>
            <td><?php echo $contact['Contact']['city']; ?></td>
            <td><?php echo $contact['Contact']['state']; ?></td>
            <td><?php echo $contact['Contact']['zip']; ?></td>
            <!--<td><?php echo $this->HTML->link('Edit', array('action'=>'update',
                $contact['Contact']['id'])) ?>
            </td>
            <td><?php echo $this->HTML->link('Delete', array('action'=>'delete',
                $contact['Contact']['id'])) ?>
            </td>-->
        </tr>
        <?php endforeach ?>
        <?php unset($contact) ?>
    </table>
    <?php echo $this->Html->link('Add Contact', array('action'=>'add')) ?>
4

0 回答 0