Zend Framework三次显示视图

时间:2013-12-09 11:36:10

标签: php zend-framework views

我遇到Zend Framework 1.11.11的问题。当我调用一个Action时,视图显示3次,如下所示:

测试和脚本/操作名称索引

测试和脚本/操作名称索引

测试和脚本/操作名称索引

所有ActionControllers都会出现这种情况。在“views / scripts / test”中,de .phtml只有一个句子。 我不知道为什么。 你可以帮帮我吗? 谢谢!

这是我的bootstrap.php

<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    /**
     * Loading configuration from ini file
     */

  protected function _initView() {  

        $view = new Zend_View();  
        $view->setEncoding('UTF-8');  
        $view->doctype('XHTML1_STRICT'); 
        $view->headTitle('My first zenRaul Framework app'); 
        //$view->headMeta()->appendHttpEquiv('Content-Type','text/html;charset=utf-8');  

        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');  
        $viewRenderer->setView($view);  

       // return $view;  
    } 


}

更新:application.ini

[production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 1
phpSettings.date.timezone = "Europe/Madrid"

resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1

UPDATE2:/var/www/ZendProject/public/index.php

<?php

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
            ->run();

$rdir = realpath(dirname('SCRIPT_NAME'));

set_include_path($rdir . '/library' . PATH_SEPARATOR . get_include_path());


try {
    // use Autoloader to load the Zend classes
    // instead of Zend_Loader::loadClass('Zend_Controller_Front');
    // a smarter alternative for larger projects
    require_once('Zend/Loader/Autoloader.php');
    $autoloader = Zend_Loader_Autoloader::getInstance();
    $autoloader->setFallbackAutoloader(true);

    $fcontroller = Zend_Controller_Front::getInstance();

    $fcontroller->throwExceptions(TRUE);
    $fcontroller->setParam('noErrorHandler', TRUE);
    $fcontroller->setControllerDirectory("$rdir/application/controllers");

    $fcontroller->dispatch();

} catch (Exception $e) {
    $contentType = 'text/html';
    header("Content-Type: $contentType; charset=utf-8");
    print 'An unexpected error occurred:';
    print '<h2>Unexpected Exception: ' . $e->getMessage() . '</h2><br /><pre>';
    print $e->getTraceAsString();
}

2 个答案:

答案 0 :(得分:0)

我不知道你必须对application.ini这样的设置做些什么,或者如果我要建议解决你的问题但是要在你的引导程序中获取视图我使用下面的代码片段。

protected $_view;

protected function _getView() {
    if ($this->_view == null) {
        $this->_view = Zend_Layout::startMvc()->getView();
    }

    return $this->_view;
}

这位于我的Bootstrap.php文件中。所以每当我需要获取视图时,我只需简单地调用$this->_getView()。为我创建一个新的视图对象似乎使问题复杂化。我希望这会有所帮助。

答案 1 :(得分:0)

解决了! index.php中的这一部分导致了这个问题!我删除它谢谢!

try {
    // use Autoloader to load the Zend classes
    // instead of Zend_Loader::loadClass('Zend_Controller_Front');
    // a smarter alternative for larger projects
    require_once('Zend/Loader/Autoloader.php');
    $autoloader = Zend_Loader_Autoloader::getInstance();
    $autoloader->setFallbackAutoloader(true);

    $fcontroller = Zend_Controller_Front::getInstance();

    $fcontroller->throwExceptions(TRUE);
    $fcontroller->setParam('noErrorHandler', TRUE);
    $fcontroller->setControllerDirectory("$rdir/application/controllers");

    $fcontroller->dispatch();

} catch (Exception $e) {
    $contentType = 'text/html';
    header("Content-Type: $contentType; charset=utf-8");
    print 'An unexpected error occurred:';
    print '<h2>Unexpected Exception: ' . $e->getMessage() . '</h2><br /><pre>';
    print $e->getTraceAsString();
}