Add your own library to your new Zend Framework Application
zend_application, zend framework
I put a lot of stuff in my own library, so I need to add this to the project. My library prefix is Amz, but it can be whatevery you want. So, let's add it to our project.
First of all, create the directory:
mkdir library/Amz
Then add a line to the config file (application/configs/application.ini). Since the library name won't be different between stages, I've put it in the base section (production):
[production] 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" autoloadernamespaces.amz = "Amz_" [staging : production] [testing : production] phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1 [development : production] phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1
It is very important to note 2 things:
1. I had to add something (doesn't matter what) after autoloadernamespaces. If I would have written 'autoloadernamespaces = "Amz_"', this would have yielded errors since an array is expected. By adding the ".amz" your making autoloadernamespaces into an array.
2. Instead of just "Amz" for my class prefix, I've added "Amz_". The reason for this is to avoid that "Amz111_" would also be accepted for my classes.
First time I tried adding my library I ran into both of these issues.
You might remember that in the virtual host we set up in the previous post, that we set the "APPLICATION_ENV" environment variable to development. Well, it comes into play here as well. APPLICATION_ENV decides what part of the config is loaded, so in our case it's the development part. When later on you set up your production environment, you set the variable to "production" and automatically it will use your production config. This enables you to have the same code on all your environments without the hastle of copying/modifying ini files liek in the old days.






+32 475 62.42.64
DragonBe
2009-05-26 13:04Although, if you also move modules/controllers into your private library namespace, you do need to prefix them (e.g. Mycomp_Modules_Controllers_IndexController).
On another note, you do need to specify your modules directories (if using modules), so you can specify your own library locations right in the bootstrap.
Be sure to keep things as generic as possible so they are testable using PHPUnit and Zend_Test (they will make use of your bootstrap!).
For that reason, add another section to your config.ini for testing purposes.
[test: production]
Have PHPun,
Michelangelo