bootstrap 是如何工作的,尤其是在 Zend Framework 中? [英] How bootstrap works in general and particularly in Zend Framework?

查看:37
本文介绍了bootstrap 是如何工作的,尤其是在 Zend Framework 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 Zend Framework 手册,但无法理解引导是如何工作的,尤其是在 ZF 和一般情况下.他们写道:

I’m reading Zend Framework manual and cannot understand how bootstrapping works particularly in ZF and in general. They write:

您的 Bootstrap 类定义了要使用的资源和组件初始化.

Your Bootstrap class defines what resources and components to initialize.

好的.这意味着应该首先实例化 Bootstrap 类.但是后来他们写了关于配置 ini 文件的内容.并且有关于 Bootstrap 类本身的指令:

Ok. It means that the Bootstrap class should be instantiated the first of all. But then then they write about the configuration ini file. And there are directives about the Bootstrap class itself in it:

1.  bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
2.  bootstrap.class = "Bootstrap"

据我所知,这意味着首先实例化的不是 Bootstarp 类.首先必须读取配置文件,获取有关 Bootstrap 类的信息,并让该信息进行实例化.否则就不需要在配置文件中包含 Bootstrap 类的信息.因为我可以这样做:

So as I understand it means that it is not the Bootstarp class that is instantiated the first of all. The first of all something has to read the configuration file, get the info about the Bootstrap class and having that info to instantiate. Otherwise there is no need to have info about the Bootstrap class in configuration file. Because I can just do this:

require_once(/application/bootstrap.php) 
$b = new Bootstrap();

并且 Bootstrap 被实例化.

and Bootstrap is instantiated.

但是他们没有提及读取配置文件然后创建Bootstrap的实例.

But they say nothing about the one that reads the config file and then makes an instance of the Bootstrap.

  1. Bootstrap 的真正工作原理是什么?
  2. 谁在哪个阶段实例化它?
  3. 他们说 APPLICATION_PATH 是一个常量.常量必须在某处定义才能使用.如果在 Bootstrap 类中使用,可以在哪里定义?
  1. How the Bootstrap really works?
  2. Who instantiates it and on which stage?
  3. They say that the APPLICATION_PATH is a constant. A constant has to be defined somewhere before it can be used. Where may it be defined if it is used in Bootstrap class?

谢谢.

推荐答案

如果您查看 ZF 随附的 index.php 文件,这应该可以回答大部分问题.

If you look at the index.php file that ships with ZF, this should answer most of those questions.

APPLICATION_PATH 常量定义在 index.php 中,这也是创建 Zend_Application 对象的地方,它只是引导应用程序,然后运行它.

The APPLICATION_PATH constant is defined in index.php, and that is also where the Zend_Application object is created which simply bootstraps the application, and then runs it.

有两种方法可以告诉您的 Zend_Application 您的引导程序在 ZF1 中的位置.

There are 2 ways to tell your Zend_Application where your bootstrap is located in ZF1.

第一种方式(显式设置):

First way (explicitly set):

$application = new Zend_Application(
    APPLICATION_ENV,
    array(
        'bootstrap' => array(
            'class' => 'Bootstrap',
            'path' => APPLICATION_PATH . '/Bootstrap.php',
        ),
        'config' => APPLICATION_PATH . '/configs/application.ini',
    )
);

在上面的例子中,引导类和引导脚本作为 $options 的一部分直接传递给 Zend_Application 的构造函数,以及 application.ini 文件.

In the above example, the bootstrap class and the bootstrap script are passed as part of the $options directly to Zend_Application's constructor, along with the application.ini file.

如果你把引导类和脚本放在你的 application.ini 文件中,那么你可以像这样初始化 Zend_Application:

If you put the bootstrap class and script in your application.ini file, then you can initialize Zend_Application like so:

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

Zend_Application 将处理 application.ini 文件并从那里收集 Bootstrap 信息.

Zend_Application will process the application.ini file and gather the Bootstrap information from there.

然后您可以调用 $application->bootstrap()->run(); 来运行应用程序.

You can then call $application->bootstrap()->run(); to run the application.

直接回答您的问题:

  1. 引导程序设置您的应用程序.处理完 ini 文件后,这是第一件事.这将为您的 ZF 应用程序设置所有必需的组件(例如前端控制器、Zend_View、布局、数据库连接等).
  2. index.php 在一开始就实例化了它.
  3. APPLICATION_PATH 立即在 index.php 中定义
  1. The bootstrap sets up your application. After processing your ini file, it is the first thing take takes place. This sets up all the required components for your ZF application (e.g. Front Controller, Zend_View, Layouts, DB connection etc).
  2. index.php instantiates it pretty much at the very beginning.
  3. APPLICATION_PATH is defined immediately in index.php

这篇关于bootstrap 是如何工作的,尤其是在 Zend Framework 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆