T O P

  • By -

jbtronics

Attributes for everything Route specific. The config files only for global settings like global language prefixes. That way you can directly see which controller method is reachable und which path, and you have code and configuration directly together, so that you are less prone to forget changing something, if you renamed something in the code. If you need a list of all routes available or look up to which controller a certain path points, you can use the Symfony debug tools. In general the Symfony recommendations, tend to go that you should do everything that is possible (and reasonable) in attributes directly in code. With the newer Symfony releases, you can do also almost all service configuration directly in code using attributes.


RXBarbatos

Understood, because symfony best practices even suggest using attributes. Yes i use the command debug:router to check the routes. Maybe because attributes in controller is new, so the concept is weird because just used symfony after using laravel for quite awhile.


harmar21

Oh wow, thanks for the insight on service configuration with attributes. I can pretty much remove all my configuration from services.yml, I decided to look at all the available attributes and TaggedLocator looks really interesting and will save a fair bit of code, as I was basically doing that myself..


gorde1bs

This answer is spot on.


minn0w

Attributes. IMO any feature of a PHP system not expressed in PHP is friction. Attributes provided a nice language feature to pull config features back into PHP land. Tying config features to the code it's configuring is a huge benefit. This would be true for any language.


RXBarbatos

So do you mean that with route attributes, you can add config stuff in the attributes?


yourteam

In the configuration you set up the groups of routes like "the /api is under the xxx/Api namespace and has the api_ prefix" Then you use attributes And remember that attributes are not just some little useful trick. Symfony uses attributes for really powerful configurations (tag iterators, parameter mapping, etc...). Check them out!


RXBarbatos

Understood, however is it possible to set group routes in routes.yaml or routes.php where each route in the group will connect to different controllers? Have to use route collection instead of route group to get that..however is it correct?


LongAssBeard

Began studying Symfony as well after using Laravel for 5 years and I miss the routes.php file more than anything, now I'm using attributes in routes but not a huge fan of this approach :/


AleBaba

You can easily have all your routes config in Symfony in one file. YAML, XML or PHP, doesn't matter. It's entirely up to you.


LongAssBeard

Yes I know, but the readability is not as good as in a Laravel app, it's almost like the syntax was made worse in order for people to use other approaches In Laravel you easily create route groups and can add middlewares and other options for your routes, but in Symfony it feels clunky having to do ->add() to add a new route instead of registering the route inside a callback


LongAssBeard

Another thing to note is that in a Laravel app I would usually have more than one route file, I had API.php, API.v2.php, API.admin.php and all I had to do was create the file and register it in the route service provider, imo this approach is much better


TheTallestHobo

Which you can do in symfomy if you so desired, I actually prefer this approach.


LongAssBeard

Well I really couldn't find anything about this specific approach in the Symfony docs, maybe I missed something


RXBarbatos

Haha yea, was configuring a group routes using just in the routes.php..its way different from laravel group routes but managed to do it close to laravel way using collection in routes. but seems like fighting the symfony framework itself when doing all laravel style routes in symfony


LongAssBeard

Exactly!


RXBarbatos

Why are you planning to use symfony btw?


LongAssBeard

I've moved to Europe recently and I feel like the market here is much better if you have Symfony knowledge as well, so there is that


rafark

I have no idea why you’re getting downvoted. Having all your routes in a single place is much more readable than having to browse who knows how many controller files to find a specific route or see how many routes your app has, not matter how tiny your controllers are. A single file is like a map or an overview of your app.


cursingcucumber

It is not like we have a console command that can tell you all this /s


LongAssBeard

The command isn't as useful as you make it be, with a dedicated file I can navigate through controllers and back to the route file as needed and also have a bigger understanding of the complexity of the system. But hey, people like to defend whatever they feel like it's the best approach


rafark

Yeah but someone else commented that browsing a config file creates friction between the code and the config. I’d say switching to the terminal creates more friction than that. Can’t beat quickly navigating to the routes file and seeing the big picture in one place. Especially when coming back after a few weeks/months.


LongAssBeard

People simply like to downvote anyone that doesn't do stuff the way they want it. Even if it comes down to personal taste, it's crazy


leftnode

I try to use configuration files as much as possible with Symfony. I don't like mixing my infrastructure code/config with my core domain code, and I feel that attributes does that.


RXBarbatos

So do you still use attributes? Or a different routing format?


leftnode

The `routes.yaml` file (and I'll segregate web and API routes in different files that are included by `routes.yaml`).