This was a bit of a pain to get going originally, but once it’s up, it works great!
I used composer which generated the autoload.php file used at the top, and from there I used Symfony2’s ClassLoader for autoloading my own annotation classes & other classes which are being scanned & reflected.
Code below;
<? require_once( "vendor/autoload.php" ); use Doctrine\Common\Annotations\AnnotationRegistry; use Doctrine\Common\Annotations\FileCacheReader; use Doctrine\Common\Annotations\AnnotationReader; use Symfony\Component\Config\FileLocator; use Symfony\Component\Routing\Loader\AnnotationClassLoader; use Symfony\Component\Routing\Route; use Symfony\Component\ClassLoader\UniversalClassLoader; // ------------------------------------------------------------------------------ class MyAnnotationClassLoader extends AnnotationClassLoader { protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot) { // TODO: Implement configureRoute() method. echo 'configure route here' . "\n"; } } $loader = new UniversalClassLoader(); $loader->registerNamespace('Symfony', __DIR__.'/vendor/symfony/routing'); $loader->registerNamespace('Entities', __DIR__.'/controllers'); $loader->register(); AnnotationRegistry::registerLoader(array($loader, "loadClass")); // ------------------------------------------------------------------------------ $reader = new FileCacheReader( new AnnotationReader(), __DIR__ . "/cache", $debug = true ); $locator = new FileLocator(); $annotationLoader = new MyAnnotationClassLoader($reader); $dirLoader = new \Symfony\Component\Routing\Loader\AnnotationDirectoryLoader($locator, $annotationLoader); $routes = $dirLoader->load(__DIR__ . '/controllers'); print_r($routes);
Basically you need to make sure your classes are saved as per http://symfony.com/PSR0, and then you can start going ahead and using the @Route(…) annotation as needed.
See;
- http://symfony.com/doc/current/components/routing/introduction.html (used with the annotations which are pulled out and then converts them into Route objects / RouteCollection)
- http://docs.doctrine-project.org/projects/doctrine-common/en/latest/reference/annotations.html (used to extract the annotations in your classes)
- http://symfony.com/doc/current/components/class_loader.html (used for auto-loading)
- http://docs.doctrine-project.org/en/latest/reference/caching.html (to replace the ‘file’ caching used above with something like APC / Memcached / Redis / etc)