0

I recently looked into the Symfony framework as I'd like to use it for some projects, however noticed that the routing is extremely awkward to use due to something called "annotations" which are basically comments above each controller method.

Is it possible to create a routes.php file and use that to overwrite the routing and completely disable these annotations? As I would like all routes in one file so it is much more flexible and maintainable.

3
  • 1
    Others consider routing annotations extremely convenient. Commented Jun 12, 2016 at 17:31
  • Before you go this way, I've used both annotations/yml for routing in very large projects and annotations actually is far less of a PITA, in my experience. Commented Jun 12, 2016 at 22:07
  • 1
    I know many frameworks which manage routes in a single PHP file (my personal preference and the most maintainable IMO), Symfony was the first one which didn't and seemed like it broke the whole purpose. One example is if you wanted to reroute the enter site, you could simply direct all routes to one location, not to mention comments that actually affect the state of an application??! That was never the purpose comments were designed for Commented Jun 12, 2016 at 22:41

2 Answers 2

2

You can use xml, yaml, php or annotation for route definitions. You just need to change this line in app/config/config.yml from

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

to

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

create app/config/routing.php and remove app/config/routing.yml

Sign up to request clarification or add additional context in comments.

3 Comments

The file "routing.yml" does not exist (in: C:\inetpub\wwwroot\Sym\demo\app/config, C:\inetpub\wwwroot\Sym\demo\app/Resources) in routing.yml (which is being imported from "C:\inetpub\wwwroot\Sym\demo\app/config/routing_dev.yml").
That's interesting... how did you created the application? I presume you used symfony-demo, but the file exists there as well ... BTW you should modify routing_dev.yml too to import routing.php as the _main resource at the end to have a proper dev environment as well (whit the toolbar).
Yup well I created a new empty application, I just had to change the _main in the dev.yml too like you said and works as expected now :)
2

Annotations (for routing but also for other configurations) are absolutely not mandatory in Symfony. As you can see in the docs, you can use YAML to define your routes. Anyway, using annotations is a best practice and I advice you to use them, since it's more immediate to get what route is bound to an action. You can always get a list of all routes with debug:routing command

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.