Zend框架显示了三次视图


Zend Framework showing views three times

我对Zend Framework 1.11.11有问题。当我调用Action时,视图显示3次,如下所示:

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

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

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

它发生在所有ActionControllers上。在"视图/脚本/测试"中,有一个只有一句话的de.phtml。我不知道为什么。你能帮我吗?非常感谢。

这是我的引导程序.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

更新2:/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();
}

我不知道你对application.ini这样的设置还做了什么,也不知道我的建议是否能解决你的问题,但为了在引导程序中获得视图,我使用了下面的片段。

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

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

已解决!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();
}