Amazium bvba, your online partner
Add your own library to your new Zend Framework Application
  • Share post with Twitter
  • Share post with StumbleUpon
  • Share post with Delicious
  • Share post with Digg
  • Share post with Technorati
  • Share post with Blinklist
2009-05-26 12:03

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.

Comments and Feedback
When using Zend_Loader::registerAutoload() and using the preferred Zend Framework directory structure you don't need to explicitly specify your library prefix.

Although, 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
Hi Mike!

I'm using the preferred Zend directory structure for models, controllers, etc... but I also have my own classes which extend the Zend functionality. These are part of the Amz library.

If I create a class Amz_Test and want to use it in my application, I need to add the Amz namespace to autoloadernamespaces or it won't be autoloaded. Under the hood, it will be added to the autoloader (more info in the manual).

The modular code will be added in one of the next posts in the series I'm writing. I'm currently experimenting with it. ;-)

I wasn't planning on having testing as part of the scope of the series... but I might pick your brain on it to get some ideas for a post. Maybe in Amsterdam in 2 weeks?

See you soon,

Jeroen

Add a Comment

Your email is never published or shared. Required fields are marked*