Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How bootstrap works in general and particularly in Zend Framework?

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

Your Bootstrap class defines what resources and components to initialize.

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"

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();

and Bootstrap is instantiated.

But they say nothing about the one that reads the config file and then makes an instance of the 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?

Thank you.

like image 555
Green Avatar asked Feb 20 '23 04:02

Green


2 Answers

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

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.

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',
    )
);

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.

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 will process the application.ini file and gather the Bootstrap information from there.

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

To directly answer your questions:

  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
like image 79
drew010 Avatar answered Feb 22 '23 23:02

drew010


Here is my short explanation:

Bootstrap initializes framework resources, initializes framework configuration, loads classes and other framework preparation work, it is started in every request, started by index.php file in default configuration.

  1. First index.php is called where APPLICATION_PATH constant is defined and is passed to Application class constructor.
  2. Application class instance created in constructor framework reads configuration file and stores for later use.
  3. Then framework bootstrapping begins:

    I. Application/Bootstrap class instance created (Framework starts creating required plugins, resources which was defined in your *.ini file.)

    II. Takes your Custom bootstrap class and initializes methods with init prefix.

    III. Loads FrontController resource, and front controller starts request dispatching process (Loads module, controller, views, calls plugin callbacks).

Overall you can track everything by yourself start from index.php file, then to library and try understand what classes are loaded, in what order.

like image 31
Aurimas Ličkus Avatar answered Feb 22 '23 23:02

Aurimas Ličkus