Disclaimer: This series of posts is designed to be used with Kohana 3.0.x and not 3.1.x . There are significant changes in 3.1.x so some of the code may not work correctly without modification.
One of the best features of Kohana 3 is the ability to easily define multiple environments and be able to load different configurations easily. There are at least two environments that you should configure when starting your project, Development and Production :
Your Development environment is your local machine that you use when you actively writing code.
Your Production environment is the code live on the server to be used by your users.
An optional 3rd environment would be a Staging environment. The Staging environment is basically a clone of your Production environment (in terms of OS and configuration) that is not accessible to your users. You would use the staging environment to test connections and run functional tests before pushing the changes live. The Staging environment is optional in my opinion because it is sort of over kill for small apps, but you can add it in as you scale.
In order to start using Kohana environments, you need to detect which environment you are in. Here is an example of code I put in my bootstrap.php . It checks if Im on localhost or not and sets the environment appropriately:
|1 2 3 4 5 6 7||
Once you have this in place, you can check the Kohana::$environment variable and make choices in configuration. You can use this technique to change your Kohana::init based on your environment and automate changing of settings. Here is an example of how you can use it in your bootstrap.php
|1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17||
You can use this technique outside of bootstrap.php as well. You will want to change your database connection configuration based on environment as well. In your database config file, you can use the same conditional statement from above to automate the changing between development and production databases. Another helpful place to use this conditional approach is with analytics. You will want analytics tracking turned off while running in development, so you do not taint your stats while testing. You can conditionally check if your running in Production and only add your tracking code then. Here’s an example I would use in a Footer view: