2016-04-15 9 views
4

私は、学習の練習としてSymfonyコンポーネントの上に構築された独自のPHPフレームワークを作成しています。私はhttp://symfony.com/doc/current/create_framework/index.htmlにあるチュートリアルにしたがってフレームワークを作成しました。Symfony3アノテーションルートのフックアップフックアップ

私は、注釈を使用してコントローラに対して自分のルートを配線したいと考えています。私は

https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Routing/Loader/AnnotationDirectoryLoader.php

:私は注釈をロードするために以下のクラスに遭遇してきたが、私はそれを使用するかどうかはわかりません

// 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())); 

:私は現在、セットアップへのルーティングを次のコードを持っています誰かが助けてくれたらそれを感謝します。

ありがとうございました

答えて

4

私はついにこれを手に入れました。私は、次のautoload.phpファイルが含まれている最初に私が変更:

use Doctrine\Common\Annotations\AnnotationRegistry; 

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

AnnotationRegistry::registerLoader([$loader, 'loadClass']); 

を次に私が(問題の)ルートコレクションのビットを変更:

$reader = new AnnotationReader(); 

$locator = new FileLocator(); 
$annotationLoader = new AnnotatedRouteControllerLoader($reader); 

$loader = new AnnotationDirectoryLoader($locator, $annotationLoader); 
$routes = $loader->load(__DIR__ . '/../Controllers'); // Path to the app's controllers 

ここAnnotatedRouteControllerLoaderのためのコードは次のとおりです。

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

これはhttps://github.com/sensiolabs/SensioFrameworkExtraBundle/blob/master/Routing/AnnotatedRouteControllerLoader.phpから取得されています。追加の注釈をサポートするように修正することをお勧めします。

こちらがお役に立てば幸いです。

関連する問題