关于路由组件的 Symfony2 文档?
Symfony2 docs on Routing component?
我正在寻找路由组件的文档,明确说明它接受哪些类型的参数。
例如,我刚创建的应用程序 routing.yml 中的 type: annotations
让我想看看那里还有哪些其他类型,但没有关于它的文档。我只能在 Book 中找到文档,在 Components 中找到一点点。
装载机类型
路由加载器的主要类型在the component's docs中描述。它提到了很多装载机:
- YamlFileLoader
- XmlFileLoader
- PhpFileLoader
- ClosureLoader
- AnnotationFileLoader, AnnotationClassLoader & AnnotationDirectoryLoader
您将在 Symfony\Component\Routing\Loader namespace
中找到所有核心加载程序
都是基于Config's component loaders, so it's worth if you also read about the Config component.
每个加载器的supports()
方法会告诉你加载器实际在什么情况下使用。例如,对于 YamlFileLoader
它是:
public function supports($resource, $type = null)
{
return is_string($resource)
&& 'yml' === pathinfo($resource, PATHINFO_EXTENSION)
&& (!$type || 'yaml' === $type);
}
您可以看到它查看资源的扩展名和类型。
自定义加载器
您可以通过实现 Symfony\Component\Config\Loader\LoaderInterface.
来实现自己的加载器
阅读 How to Create a custom Route Loader cookbook. It actually explains quite a lot on how routing loaders work. Have a look at some 3rd party loaders too, such as the FOSRestBundle 中的更多信息。
如何将它们连接在一起
查看 Symfony 标准版中生成的容器,了解完整堆栈框架如何将它们连接在一起。它应该类似于:
/**
* Gets the 'routing.loader' service.
*
* This service is shared.
* This method always returns the same instance of the service.
*
* @return \Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader A Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader instance.
*/
protected function getRouting_LoaderService()
{
$a = $this->get('file_locator');
$b = $this->get('annotation_reader');
$c = new \Sensio\Bundle\FrameworkExtraBundle\Routing\AnnotatedRouteControllerLoader($b);
$d = new \Symfony\Component\Config\Loader\LoaderResolver();
$d->addLoader(new \Symfony\Component\Routing\Loader\XmlFileLoader($a));
$d->addLoader(new \Symfony\Component\Routing\Loader\YamlFileLoader($a));
$d->addLoader(new \Symfony\Component\Routing\Loader\PhpFileLoader($a));
$d->addLoader(new \Symfony\Component\Routing\Loader\AnnotationDirectoryLoader($a, $c));
$d->addLoader(new \Symfony\Component\Routing\Loader\AnnotationFileLoader($a, $c));
$d->addLoader($c);
return $this->services['routing.loader'] = new \Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader($this->get('controller_name_converter'), $this->get('monolog.logger.router', ContainerInterface::NULL_ON_INVALID_REFERENCE), $d);
}
这里的关键是 LoaderResolver,它负责为某种配置找到合适的加载程序。
我正在寻找路由组件的文档,明确说明它接受哪些类型的参数。
例如,我刚创建的应用程序 routing.yml 中的 type: annotations
让我想看看那里还有哪些其他类型,但没有关于它的文档。我只能在 Book 中找到文档,在 Components 中找到一点点。
装载机类型
路由加载器的主要类型在the component's docs中描述。它提到了很多装载机:
- YamlFileLoader
- XmlFileLoader
- PhpFileLoader
- ClosureLoader
- AnnotationFileLoader, AnnotationClassLoader & AnnotationDirectoryLoader
您将在 Symfony\Component\Routing\Loader namespace
中找到所有核心加载程序都是基于Config's component loaders, so it's worth if you also read about the Config component.
每个加载器的supports()
方法会告诉你加载器实际在什么情况下使用。例如,对于 YamlFileLoader
它是:
public function supports($resource, $type = null)
{
return is_string($resource)
&& 'yml' === pathinfo($resource, PATHINFO_EXTENSION)
&& (!$type || 'yaml' === $type);
}
您可以看到它查看资源的扩展名和类型。
自定义加载器
您可以通过实现 Symfony\Component\Config\Loader\LoaderInterface.
来实现自己的加载器阅读 How to Create a custom Route Loader cookbook. It actually explains quite a lot on how routing loaders work. Have a look at some 3rd party loaders too, such as the FOSRestBundle 中的更多信息。
如何将它们连接在一起
查看 Symfony 标准版中生成的容器,了解完整堆栈框架如何将它们连接在一起。它应该类似于:
/**
* Gets the 'routing.loader' service.
*
* This service is shared.
* This method always returns the same instance of the service.
*
* @return \Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader A Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader instance.
*/
protected function getRouting_LoaderService()
{
$a = $this->get('file_locator');
$b = $this->get('annotation_reader');
$c = new \Sensio\Bundle\FrameworkExtraBundle\Routing\AnnotatedRouteControllerLoader($b);
$d = new \Symfony\Component\Config\Loader\LoaderResolver();
$d->addLoader(new \Symfony\Component\Routing\Loader\XmlFileLoader($a));
$d->addLoader(new \Symfony\Component\Routing\Loader\YamlFileLoader($a));
$d->addLoader(new \Symfony\Component\Routing\Loader\PhpFileLoader($a));
$d->addLoader(new \Symfony\Component\Routing\Loader\AnnotationDirectoryLoader($a, $c));
$d->addLoader(new \Symfony\Component\Routing\Loader\AnnotationFileLoader($a, $c));
$d->addLoader($c);
return $this->services['routing.loader'] = new \Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader($this->get('controller_name_converter'), $this->get('monolog.logger.router', ContainerInterface::NULL_ON_INVALID_REFERENCE), $d);
}
这里的关键是 LoaderResolver,它负责为某种配置找到合适的加载程序。