A web application goes through some stages when growing up. Generally, it starts from development and ends at production. There can be some more stages within this two ends. And, in maximum cases this stages are overlapped. The overlapped stages can be hosted in different servers with different settings but shares the same code. In this situations, if we want to keep bootstrap in SVN repo, it needs to setup bootstrap in a little different way. So that, it can handle many server settings with a single bootstrap.
For example, just now I am working on an application which is being developed in 3 local machines (me and two other members of my team) and being tested in our staging server. Besides, our client is monitoring progress of current sprint in their staging server and testing completed sprints in their pre-production server. Here, except the production servers, all hosts contains a checked out copy of code from a single repository. But this hosts have some difference in their library path, database host and authentication etc.
If my assumptions are ok for you, let’s start after downloading.
I have assumed that you are familiar with Zend Framework. And already worked on (atleast one) ZF MVC application. You know the basic things about bootstrapping and using the most common directory structure (shown in image). However, you can tune this technique for your favorite Zend directory stracture.
Downoad the files from here. The zip file contains –
- index.php – This is the bootstrap file. Your .htaccess file points all request to this file.
- config.xml – Contains all configurations of your application, for all hosts.
- Bootstrap.php – A php class that handles all startup settings in a vary organized way. (I don’t know who first wrote it, Thanks to him)
How to bootstrap for multiple host
After extracting the zip, put the Bootstrap.php in your own library directory(library/my) and config.xml in applications configs directory. Index.php file will be generally located at myapp/public/index.php. All requests need to go through this file (Front Controller).
Just change this 3 files as your applications need. Then your application will be ready for bootstrapping for all your servers, without any change. So, lets see what to change in these files and how it will work.
First, open the index.php file. Now, in ‘List of servers’ section, list all your server paths in arrays by their application stage. For example, here I’ve listed all staging servers in $staging. Then, in ‘Define APPSTAGE’ section, define the application stage and set the include_path for this stage. If you if you have 2 or more servers with different library path but for same stage, you shouldÂ write set_include_path in conditions by checking the server-name. At end of this file, include the Bootstrap.php file (assuming in library/my folder) and run the static Bootstrap::start() method. Pass the path of config.xml file as parameter.
Then open the config.xml file. Under the root element (configdata), first child nodes will be the name of you application stages which you’ve defined in index.php. In my example, they are production, staging and development. This nodes will now contain the stage specific settings. Change this settings as your application and servers environment.
Now, look at the bootstrap class (in Bootstrap.php). When you run the Bootstrap::start() method in index.php, it loads setting for your APPSRAGE section. ThenÂ setup everything of your application using this settings.
See how this class setting up everything carefully and change as your need. For quick reference, I am just listing here the methods of this class –
- start() – Starts the bootstrapping process by calling a number of interim functions and at the end, sends the response.
- setupEnvironment() – Setup application environment by specifying debug option, query logging, output compression andÂ setting the time zone.
- prepare() – Prepare the application by loading the required Zend libraries and then calling other functions for their specific setup (Front controller,Â View, Database etc.)
- setupFrontController() – Get the instance of Zend front controller and then set a few basic options. Also specify the default controller path.
- setupView() – Get an instance of Zend view and set the unicode encoding. Then get the view renderer and set it as a view helper for further help.
- setupDatabase() – Load the DB config and then store it in Zend registry. Afterwards, use it for connection to database and setting it as default adapter of Zend_Db_Table.
- setRoutes() – Setup and manage all routings of appllication
- sendResponse() – Receive the response object and set the unicode header before sending it to the output
That’s all. feel free to change anything at any step of this process. Your comments/suggestions are very much welcome.Â If you have a better idea, please let us know. We’ll be grateful to you.