I had a need lately to include an external library of php classes in a Symfony project. On option was dumping them into the lib folder, but I didn’t want to do that. At first I tried placing a script in a filter that loaded the classes. However, this led to problems. I was serializing some objects to the $_Session variable in php. When the next request was made, Symfony would call session_start() before my filter loaded the classes. Naturally php could not find the classes. After much searching, I ran across the autoload.yml file in symfony. This file allows you to define extra classes that can be autoloaded when Symfony loads. This still didn’t quite meet my needs because I also wanted to have dynamically changing include paths. Thankfully, Symfony allows you to place php code inside a .yml file. Here is my final solution:
##apps/appName/config/autoload.yml
autoload:
makeupaname:
name: Make up a name
path: <?php echo $mydynamicvar; ?>/someFolder/
recursive: on
makeupasecondname:
name: Make up a second name
path: <?php echo $mydynamicvar; ?>someOtherFolder/
recursive: on

Pingback: A week of symfony #266 (30 January -> 5 February 2012) « We are php