index and application.ini confusion&几个简单的问题

时间:2011-09-05 04:14:23

标签: php zend-framework bootstrapping

Zend Framework新手。我一直在阅读并发现application.ini中提到的任何内容都已初始化。

1 - 我的问题如果我在ini中提到了包含库的路径,那么为什么我需要在索引文件中再次使用包含路径,如

// Include path
set_include_path(
    BASE_PATH . '/library'
);
application.ini中的

2 - 应该编写像APPLICATION_PATH“/../library”或“APPLICATION_PATH”/ library“这样的includePaths.library。记住我的索引APPLICATION_PATH变量?

3 - 我为什么要在BootStarp文件中使用_initView()。该代码有什么用途,比如

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

application.ini提到

;Include path
includePaths.library = APPLICATION_PATH "/../library"

自举

<?php

    class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
    {
        protected function _initView()
        {
            // Initialize view
            $view = new Zend_View();
            $view->doctype('XHTML1_STRICT');
            $view->headTitle('My Project');
            $view->env = APPLICATION_ENV;

            // Add it to the ViewRenderer
            $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
                'ViewRenderer'
            );
            $viewRenderer->setView($view);

            // Return it, so that it can be stored by the bootstrap
            return $view;
        }
    }

索引

<?php
define('BASE_PATH', realpath(dirname(__FILE__) . '/../'));
define('APPLICATION_PATH', BASE_PATH . '/application');

// Include path
set_include_path(
    BASE_PATH . '/library'
);

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

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

$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);

$application->bootstrap();
$application->run();

1 个答案:

答案 0 :(得分:3)

1和2是旧版Zend Framework的延迟冗余。您通常可以选择一种方法并坚持下去。

index.php

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

application.ini

includePaths.library = APPLICATION_PATH "/../library"

就个人而言,我赞成前者。

您的Bootstrap.php文件似乎也有一些较旧的ZF习惯。较新的应用程序体系结构包括视图的资源插件。只需将其放入application.ini文件

即可
resources.view.encoding = "utf-8"

并将您的引导方法更改为

// don't call this _initView as that would overwrite the resource plugin
// of the same name
protected function _initViewHelpers()
{
    $this->bootstrap('view'); // ensure view resource has been configured
    $view = $this->getResource('view');

    $view->doctype('XHTML1_STRICT');
    $view->headTitle('My Project');
    $view->env = APPLICATION_ENV;
}