如何将视图放在zend框架中

时间:2011-12-19 10:54:57

标签: php zend-framework

这是我在zend框架中的Controller编码

public function homeAction() {

    $storage = new Zend_Auth_Storage_Session();
    $data = $storage->read();
    if (!$data) {
        $this->_redirect('admin/login');
    }
    $this->view->storeid = $data->storeid;
    $iExtStoreId = $data->storeid; // admin storeid

    $db = Zend_Db_Table::getDefaultAdapter();
    $sql = "SELECT storeId FROM stores where extStoreId= '$iExtStoreId' ";

    $result = $db->fetchAll($sql);
    $sStoreId = $result[0]['storeId'];
    if (strlen($sStoreId) >= 32) {
        $sql = "select * from voucherRedemptions where storeId='$sStoreId'";

        $result2 = $db->fetchAll($sql);
    } else {
        $result2 = array();
    }
    echo "<table border='1'>
                        <tr>
                        <th>redemptionId</th>
                        <th>voucherId</th>
                        <th>storeId</th>
                        <th>redemptionTime</th>
                </tr>";

    if (count($result2) > 0) {
        foreach ($result2 as $row) {
            echo "<tr>";
            echo "<td>" . $row['redemptionId'] . "</td>";
            echo "<td>" . $row['voucherId'] . "</td>";
            echo "<td>" . $row['storeId'] . "</td>";
            echo "<td>" . $row['redemptionTime'] . "</td>";
            echo "</tr>";
        }
    } else {
        echo "<tr><td colspan='4'>Data not found </td></tr>";
    }

    echo "</table>";
}

现在我想将视图code保留在view文件夹home.html what i can do ?

2 个答案:

答案 0 :(得分:1)

不要echo控制器中的所有内容。这就是view的用途。

设置所有viewvariables后:

$this->render('/path/to/home.html');

打印那里的所有东西。

另请注意,Zend Framework有pretty good documentation available

答案 1 :(得分:1)

我对Zend Framework没有太高的速度,但是继续PeeHaa的回答和粗略的知识,我猜你的控制器看起来像这样:

class YourController {

    public function indexAction() {
        $view = new Zend_View();
        $view->someVar = 'Some Value.';
        $view->render('/admin/admin.phtml');
    }
}

然后在您的admin.phtml模板中

<!-- should output "Some Value." -->
<p><?php echo $this->someVar; ?></p>