php - index 和 application.ini 混淆以及一些简单的问题

标签 php zend-framework bootstrapping

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

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

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

2 - 在 application.ini 中我应该写 includePaths.library 像 APPLICATION_PATH "/../library"或 APPLICATION_PATH "/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 和 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 习惯。较新的应用程序架构包括 View 的资源插件。只需将其放入您的 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;
}

关于php - index 和 application.ini 混淆以及一些简单的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7303841/

相关文章:

php - 如何从 MySQL 数据库搜索和显示数据

java - 使用 HttpURLConnection 在 Android 中更新 mySql 表

php - 创建在网页上显示Codeigniter日志文件的页面

php - Zend PDO 错误 - 我该如何调试它?

javascript - 了解 Laravel 混合

php - 将表 A 中复选框选定的数据插入到表 B 中

php - zend框架FlashMessenger问题

php - Zend PDF - 有没有一种很好的方法来实现 MVC 模式?

java - 使用 Bootstrap 在部署时创建目录

javascript - 如何使用 javascript 将 Bootstrap 图标添加到 Canvas ?如果有办法