问题
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.
回答1:
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"
And in this routing.yml
you import your Controllers
with annotation
routing:
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 }
回答2:
Of course you can, but also you can configure the router in order to generate your desired urls.
Here is an example for sending emails from a console command: http://symfony.com/doc/current/cookbook/console/sending_emails.html
On console commands there is no request or webserver configuration. So this is another clear method to set up routes programmatically. I hope it will help you.
来源:https://stackoverflow.com/questions/28860091/can-you-use-both-annotation-and-yaml-routing