Zend应用程序/引导程序如何工作?

Edw*_*Lee 5 php zend-framework

关于Zend Framework 1.9基础知识的一些问题.

  1. 我按照快速入门指南,基本上,自举涉及,

    一个.来自index.php:

    $ZEND_FRAMEWORK_LIB_PATH = '/appl/ZendFramework-1.9.7/library';
    defined('APPLICATION_PATH') || define('APPLICATION_PATH', (realpath(dirname(__FILE__) . '/../application')));
    defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
    set_include_path(implode(PATH_SEPARATOR, array((dirname(dirname(__FILE__)) . '/library'), $ZEND_FRAMEWORK_LIB_PATH, get_include_path(),)));
    require_once 'Zend/Application.php';
    $application = new Zend_Application(APPLICATION_ENV, (APPLICATION_PATH . '/configs/application.ini'));
    $application->bootstrap()->run();
    
    Run Code Online (Sandbox Code Playgroud)

    湾 然后在Bootstrap.php中,我有

    protected function _initAutoload()
    {
        $autoloader = new Zend_Application_Module_Autoloader(array("namespace" => "Default_", "basePath" => dirname(__FILE__),));
        return $autoloader;
    }
    
    protected function _initDoctype()
    {
        $this->bootstrap("view");
        $view = $this->getResource("view");
        $view->doctype("XHTML1_STRICT");
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 首先,我不明白的一些事情:

    一个.如果用户不是通过默认的index.php访问该站点,这是否意味着引导(实际上,index.php中的所有代码,包括环境设置等,都将被绕过?)

    湾 没有明确调用Bootstrap _initAutoload()_initDoctype()方法的地方.那么这些方法何时被隐式调用?

    C.因为在index.php中,我已经将配置文件"传入" '/configs/application.ini'了Zend_Application构造函数,有没有办法在别处检索配置条目?

  3. 在我的应用程序中,我必须使用不同的数据库(所以我不能只使用resources.db.*).所以在同一个application.ini文件中,我有,例如

    custdb.adapter = "PDO_MYSQL"
    custdb.params.host = "localhost"
    custdb.params.username = "username"
    custdb.params.password = "password"
    custdb.params.dbname = "custdb"
    
    Run Code Online (Sandbox Code Playgroud)

    管理数据库适配器的最佳做法是什么?

    一个.是否有可能(并且应该)在index.php或Bootstrap.php中创建数据库适配器,并在需要时(以及如何)在其他地方检索它?

    湾 或者可以(并且我应该)只在其他地方检索配置条目(如何?)并在需要时实例化数据库适配器?

谢谢!

Ben*_*hen 7

这里有几个答案.

2A.所有请求都被重定向到index.php.这是通过mod_rewrite完成的,并在.htaccess文件中指定.

2B.引导程序调用前缀为的任何方法_init.见 Zend框架 - 操作理论

2C.是.Zend :: Config.您可以存储实例Zend::Registry以便于访问.例如:

$config = new Zend_Config((APPLICATION_PATH . '/configs/application.ini')); 
$application = new Zend_Application(APPLICATION_ENV, $config);
Zend_Registry::set('config', $config);
Run Code Online (Sandbox Code Playgroud)

检查API参考以查看这两个类的构造函数.

我不认为快速启动是有帮助的.我建议买一本书.我很喜欢Keith Pope的"Zend Framework 1.8 Web应用程序开发".