可以同时使用注释和YAML路由吗? [英] Can you use both Annotation and YAML routing?

查看:109
本文介绍了可以同时使用注释和YAML路由吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些使用注解路由的symfony代码。我需要对现在通过parameters.yml中的设置获取主机的路由进行更改。我看到,如果您在routing.yaml中定义路由,这是可能的: http:// symfony.com/doc/current/components/routing/hostname_pattern.html 因此,我想知道是否可以同时使用在注解和routing.yaml中定义的路由,或者是否只能使用一个或另一个(换句话说,我将不得不将束中的所有路由更改为routing.yaml)?尽管我猜不建议同时执行这两项操作,但要保持代码干净。

I have some symfony code that is using annotation routing. I need to make a change to the routes where the host is now gotten from a setting in parameters.yml. I saw that this is possible if you define routes in routing.yaml: http://symfony.com/doc/current/components/routing/hostname_pattern.html So I was wondering if it were possible to use routes defined in annotations and routing.yaml at the same time, or if you can only do one or the other (so in other words, I will have to change all the routes in the bundle to be in routing.yaml instead)? Though I'm guessing it's not recommended to do both, to keep the code clean.

推荐答案

是的,您可以同时使用两者。实际上,Symfony标准版中发生的只是在 config.yml 中导入主要的 routing.yml

Yes, you can use both. In fact what is happening in Symfony standard edition is just importing main routing.yml in config.yml:

framework:
    router:
        resource: "%kernel.root_dir%/config/routing.yml"

在此 routing.yml 中,您导入控制器带有注释路由:

And in this routing.yml you import your Controllers with annotation routing:

路由。 yml:

your_annotation_route:
    resource: "@AcmeDemoBundle/Controller/DefaultController.php"
    type: annotation
#########  you can use regular yml routing here ########
your_yaml_route:
    path:     /
    host:     m.example.com
    defaults: { _controller: AcmeDemoBundle:Main:mobileHomepage }

这篇关于可以同时使用注释和YAML路由吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆