I often use the Zend Framework from within my Symfony applications and until recently, I’ve been using the sfZendPlugin to import and autoload the library. You’ll notice that this plugin has recently been axed leaving some people confused as to how to use the library in their projects.
Adding the Library
First, you’ll need to add the Zend Framework library. Since I like to link libraries via subversion, I’ll link the Zend Framework to my project’s lib/vendor directory. Open up a terminal to your symfony project’s root directory and type this:
svn propedit svn:externals lib/vendor/
Note: You may have to create the vendor directory (and use svn add to add it to your repository) if you don’t already have it.
This will bring up a text editor where you can add a link to an external library via subversion. Type this in:
Zend http://framework.zend.com/svn/framework/tag/release-1.5.0PR/library/Zend
… and save and close the file. This will link lib/vendor/Zend to the 1.5.0 PR release of the Zend Framework. You can, of course, use another version if you prefer. Of course, you don’t have to link this via subversion. You can simply download and copy the Zend Framework into this folder for the same effect, but using subversion is my preferred method.
Linking the Library to Symfony
All you need to do now is activate symfony’s autoloading of the library files. Open up your application’s setting.yml file (mine is at apps/frontend/config/settings.yml) and add the appropriate settings. It should look something like this:
all:
.settings:
zend_lib_dir: %SF_ROOT_DIR%/lib/vendor
autoloading_functions:
- [sfZendFrameworkBridge, autoload]
Make sure to clear your symfony cache by using:
./symfony cc
That’s it! You should be able to use any of the Zend Framework classes from within symfony without using a require statement.
Edit: As per Gerald’s comment below, the zend_lib_dir path was listed as “zend_lib_dir: %SF_ROOT_DIR%/lib/vendor/Zend” but should not included the “Zend” folder name at the end as sfZendFrameworkBridge.class.php actually adds this automatically.
Hi,
I’ve been very interested by your post linking Zend inside a symfony project, however when I tried I always have an error message that it cannot find Loader.php (although I correctly set it up exactly the way you said and if I check sf_zend_lib_dir it is actually the correct path).
I simply download Zend1.5PR and put it in lib/vendor…
Hi Gerald,
Make sure that you’re only putting the Zend *library files* in lib/vendor and not the whole download. The download comes with a bunch of extra stuff, but you really only need the library files inside lib/vendor/Zend. Let me know if that helps.
Hi Mark,
I’ve checked and yes I put all files under myproject/lib/vendor/Zend. I do have Loader.php inside this folder and all php classes from Zend but it still does not work. It keeps saying:
[sfAutoloadException]
Invalid Zend Framework library structure, unable to find Zend/Loader.php (ZF >= 0.9.0) or Zend.php (ZF < 0.9.0) library
I really can not understand what’s wrong…
Symfony goes up do the Zend bridge and cannot get the Loader.php.
(sf 1.0.11)
Hmm, I’m not sure what it could be then. I’d say double check that your zend_lib_dir path matches where you have the library installed and then clear your cache again just to be sure. I can’t really think of why else this wouldn’t work for you…
Hi Mark,
I finally found out! When I looked at sfZendFrameworkBridge.class.php it actually add Zend/ at the end of sf_zend_lib_dir. I simply declare zend_lib_dir: %SF_ROOT_DIR%/lib/vendor and it now find it and I do not get any error. I have not test to actually do something with Zend in symfony yet but I believe it should works now, I will try later and give feedback if any issue.
I am a bit surprise it works for you this way, unless you have Zend/Zend structure.
Interesting Gerald, thanks for posting an update! You may in fact be correct about the path. I used a slightly different approach in my actual project, so I’ve updated the post accordingly with your fix.
Thanks again!