Php Symfony3连接注释管线

Php Symfony3连接注释管线,php,annotations,doctrine,symfony,Php,Annotations,Doctrine,Symfony,作为学习练习,我正在编写自己的PHP框架,它构建在Symfony组件之上。我按照在上找到的教程创建了我的框架 现在,我想使用注释将我的路线与控制器连接起来。我目前有以下代码来设置路由: // Create the route collection $routes = new RouteCollection(); $routes->add('home', new Route('/{slug}', [ 'slug' => '', '_controller' =>

作为学习练习,我正在编写自己的PHP框架,它构建在Symfony组件之上。我按照在上找到的教程创建了我的框架

现在,我想使用注释将我的路线与控制器连接起来。我目前有以下代码来设置路由:

// Create the route collection
$routes = new RouteCollection();

$routes->add('home', new Route('/{slug}', [
    'slug' => '',
    '_controller' => 'Controllers\HomeController::index',
]));

// Create a context using the current request
$context = new RequestContext();
$context->fromRequest($request);

// Create the url matcher
$matcher = new UrlMatcher($routes, $context);

// Try to get a matching route for the request
$request->attributes->add($matcher->match($request->getPathInfo()));
我遇到了以下类来加载注释,但我不确定如何使用它:

如果有人能帮忙,我将不胜感激


谢谢

我终于设法让它工作起来了。首先,我将包含autoload.php文件的位置更改为:

use Doctrine\Common\Annotations\AnnotationRegistry;

$loader = require __DIR__ . '/../vendor/autoload.php';

AnnotationRegistry::registerLoader([$loader, 'loadClass']);
然后我将routes收集位(问题中)更改为:

以下是控制器加载程序的代码:

class AnnotatedRouteControllerLoader extends AnnotationClassLoader {
    protected function configureRoute(Route $route, ReflectionClass $class, ReflectionMethod $method, $annot) {
        $route->setDefault('_controller', $class->getName() . '::' . $method->getName());
    }
}
这是从一个地方拿走的。您可能希望修改它以支持其他注释

我希望这有帮助

class AnnotatedRouteControllerLoader extends AnnotationClassLoader {
    protected function configureRoute(Route $route, ReflectionClass $class, ReflectionMethod $method, $annot) {
        $route->setDefault('_controller', $class->getName() . '::' . $method->getName());
    }
}