Zend-Framework - 视图不起作用

时间:2014-05-06 16:31:09

标签: zend-framework view render

我正在制作我的第一个应用程序,而Zend和我有问题。

zftutorial.dev \应用\控制器\ IndexController.php

<?php
class IndexController extends Zend_Controller_Action
{
    public function init()
    {
        /* Initialize action controller here */
    }
    public function indexAction()
    {
        $this->view->title = "My Albums"; 
    }
}

zftutorial.dev/application/views/scripts/index/index.phtml

<?php echo $this->render('header.phtml'); ?>
<h1><?php echo $this->escape($this->title); ?></h1>
<?php echo $this->render('footer.phtml'); ?>

ZF-教程/应用/视图/脚本/ header.phtml

<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <title><?php echo $this->escape($this->title); ?></title>
</head>
<body>
<div id="content">

ZF-教程/应用/视图/脚本/ footer.phtml

      </div>
   </body>
</html>

但是当我去发言时

  

http://zftutorial.dev/

我在浏览器中收到此信息:

发生错误

申请错误

当然我设置了vhosts文件,当我使用

zftutorial.dev/application/views/scripts/index/index.phtml

<h1><?php echo $this->escape($this->title); ?></h1>

然后一切都好。问题是这个<?php echo $this->render('header.phtml'); ?><?php echo $this->render('footer.phtml'); ?>行,但我不知道出了什么问题。

3 个答案:

答案 0 :(得分:1)

试试这个:

在文件夹layouts / scripts /中创建 header.php footer.php

然后在 layout.phtml 中呈现页眉和页脚脚本。

<强> layout.phtml

<html>
<head>
<?php echo $this -> headTitle(); //get the title ?>
</head>
<body>
<?php echo $this->render('header.phtml'); //relative to layouts/scripts ?>
<?php echo $this->layout()->content; //the view script redered in the controller action?>
<?php echo $this->render('footer.phtml'); //relative to layouts/scripts ?>
</body>

<强> bootstrap.php中

添加此功能:

protected function _initViewHelpers() {
    $this->bootstrap('view');
    $view = $this->getResource('view');

    //Page Base Head Title
    $view -> headTitle('My Domain');
    //Page Head Title - Seperator
    $view -> headTitle() -> setSeparator(' - ');

    return $view;
}

<强> MyController.php

控制器操作内或 init()功能中,添加以下行:

//Prepended - My Domain
$this->view->headTitle()->prepend('Prepended');

//My Domain - Appended
$this->view->headTitle()->append('Appended');

答案 1 :(得分:1)

我认为使用布局是更好的解决方案。

但是使用您的代码,您可以这样做:
zftutorial.dev/application/views/scripts/index/index.phtml

<?php echo $this->partial('header.phtml', array('title' => $this->title)); ?>
<h1><?php echo $this->escape($this->title); ?></h1>
<?php echo $this->partial('footer.phtml'); ?>

答案 2 :(得分:0)

您的观点似乎未在ZF注册。尝试通过在Bootstrap类中放置以下方法来注册它们。

   public function _initViewScripts(){
         $this->bootstrap ( 'view' );
         $view = $this->getResource('view');
         //Replace layout/scripts with the folder which contains your header.phtml and footer.phtml
         $view->addScriptPath (APPLICATION_PATH. '/layouts/scripts/');
   }

此外,如果您已将页眉和页脚视图放在/application/views/scripts/index/目录中,请将其移至更高级目录,例如/layouts/scripts/