2

I'm trying to create my first module. I'd like to know if I can use annotation route like Symfony instead of separated files.

For example, can I use the second snippet instead of the first one?

hello_world_settings:
  path:  '/hello/world'
  defaults:
    _content:  '\Drupal\hello_world\Controller\HelloWorldController::myCallbackMethod'
  title: 'Hello World'
  requirements:
    _permission: 'access content'
/**
 * @Route("/hello/world",name="hello_world")
 * @Method({"GET"})
 */
 public function myCallbackMethod() {
2

1 Answer 1

6

AFAIK, Drupal doesn't support any of the Symfony route annotations. The code is present in vendor/, but not used by core.

The closest thing is the concept of Entity links, which are part of the Doctrine annotations on the entity class. These link names can be used in the ::toUrl() method to generate the canonical link for an entity, the edit link, etc. However, the routes still need to be defined in the routing.yml file.

2
  • 1
    As of drupal 8.0.0 we can also define a route manager for each entity.
    – Eyal
    Dec 7, 2015 at 20:28
  • @Eyal, yeah, didn't want to get into the EntityRouteProviderInterface for this. That is why tools like drupal console are handy for mapping routes to paths (I have a feature request in to also also get the entity link annotations, may work on a PR for that).
    – mpdonadio
    Dec 7, 2015 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.