问题
I've decided that rather than have a copy of the Zend Framework in each application's directory, I'd like to keep it on one location on the server, with the one copy used by all my websites. However, I'd like my app's custom classes to still be within the application folder. So a folder structure a bit like this:
webroot
|...library
| |......Zend
|
|...app1
| |.....Library
| |.......App1
|
|...app2
|.....Library
|.......App2
How can I get Zend Loader to automatically find the classes in App1 and App2? (preferably by just changing something in application.ini or bootstrap.php)
回答1:
You can create a single library directory, with symlinks to the actual live shared code:
webroot/library/Zend -> /path/to/Zend/library/Zend
webroot/library/App1 -> /path/to/App1/library/App1
webroot/library/App2 -> /path/to/App2/library/App2
Then, you only need webroot/library in your path.
To handle version updates, you can simply change the symlink to point to a new install:
webroot/library/Zend -> /path/to/Zend-test/library/Zend
回答2:
The Zend Loader will use your php include_path to find files to load.
Simply add webroot/library to your include_path (which you can either do in php.ini or in your bootstrap) and the autoloader should be able to find the framework.
If you are keen to have a shared version of Zend you may as well just use pear (http://pear.zfcampus.org/) to install it and then as long as you have your include_path set to look in your pear dir ( /usr/share/php on my machine ) then you are good to go.
I would advise only to do this for dev machines though, as others have said it's a good idea to be able to control the versions of zend for each app when in production.
来源:https://stackoverflow.com/questions/3772295/autoloading-classes-when-not-in-same-directory-as-zend-library