09 July 2009

What is in the application.ini file?

The application.ini file is like the global variables for a Zend website. It holds the settings your application will use. It is a good practice not to use globals on a website, so instead we use a registry. The purpose of the application.ini file is to save the configuration of your website, e.g. the database parameters.

The advantage of using Zend_Application is that it will automatically load the settings from your application.ini file into the application. There is basically three 'resources' that the application will load, i.e. the layout, db and frontcontroller.

Another advantage of the application.ini file is that you can set the specific settings you want to use for each environment of the application. You can use local mysql settings when you develop locally on your machine, and use the mysql settings for the server's database when it goes into testing or production.

If you want to save the application.ini to your registry, you can do that in the bootstrap file. Here is an example if you want to save the settings to the registry:

public static function _initRegistry()
  $configuration = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);
  $registry = Zend_Registry::getInstance();
  $registry->configuration = $configuration;

Tadaa! Now you can call settings from your registry what you would want to use in your application — and it can be environment specific, e.g. I use different email addresses for the contact form dependant if it's in development or not.

So, we know we can set up a ini file which contains settings, and we can save that into the registry to make it globally available to the application. Now let's look at the application.ini file itself. The file is normally saved under Application/configs/application.ini:


phpSettings.date.timezone = "YourContinent/YourCity"
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0

includePaths.library = APPLICATION_PATH "/library"

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

resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.layout.layoutPath = APPLICATION_PATH "/views/layouts"
resources.layout.layout = "layout"

resources.db.adapter = PDO_MYSQL
resources.db.params.host = dedicatedDbOnServer.net
resources.db.params.username = serverUser
resources.db.params.password = serverPwd
resources.db.params.dbname = myAppDb

emailName = officialName
emailAdd = official@address.com

[testing : production]

phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]

phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = root
resources.db.params.dbname = localDb

emailName = testingName
emailAdd = testing@address.com

As you can see, you will display errors while in development and testing, and not in production. You will also use your local mysql while in development. The file also stipulates to use a layout template if you use a 2-step view. Very nice. I like ;)

There are much more you can set in the ini file, but the above is normally enough for me.

Take care!

No comments:

Post a Comment