-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC][Routing] Added the Route attribute #37474
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i like it :)
should we offer hybrid support by default? Thus vendor controllers could specify both attributes as well as annotations. Not sure this makes sense in practice though, otherwise i'd expect an exception if attributes and annotations are mixed&matched.
edit: nevermind, this works as expected already.
we could use sibling/nested attributes for rich options perhaps. Not sure it works out.
Right now, you can mix&match attributes and annotations. It would have the same effect as if you specified multiple annotations. This was the easiest way to implement it, but this behavior is certainly debatable. |
c23533f
to
513cc5c
Compare
i think it's fair to assume both definitions remain equivalent. Then it works on all SF versions, loading either one or another.
raising the bigger question, is there any reason not to? Should we deprecate annotation support once we support php:^8? |
Please note that the attribute syntax has just changed from |
@javiereguiluz This has not been merged yet, has it? |
Not merged yet ... but approved, so it will be merged "soon" (I was notified about this by this nice @beberlei service: https://php-rfc-watch.beberlei.de/) |
All right. If I changed the PR now, Travis won't be able to run my tests anymore. I don't expect the reflection API to be affected by the change, so all I will need to do is adjust the test fixtures and the attribute class itself to the new syntax. I'll do this as soon as Travis' nightly build supports it. |
src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php
Outdated
Show resolved
Hide resolved
Pending PR for the syntax change: php/php-src#5796 |
a1962c0
to
ee74cd5
Compare
I've pushed an update to this PR the other day.
Some tests mocked Doctrine's annotation reader. I rewrote those test to use an actual annotation reader instead. It might be a good idea to backport that change, in order to ease the maintenance. |
ee74cd5
to
17e221c
Compare
I might need some help with getting Travis green: https://travis-ci.org/github/symfony/symfony/jobs/705695329 On the php 7.4 run, it looks like some piece of code is trying to parse my new attribute (which only works on php 8). Anything I can do to prevent that? |
patch |
17e221c
to
76d0e91
Compare
That did not help, apparently. 🙁 |
76d0e91
to
93e199c
Compare
@javiereguiluz Just to chime in, |
PHP attributes are basically a moving target at the moment. Just look at the changes between the alpha1 and alpha2 releases. And because I need a working testsuite, I won't anticipate any syntax changes until they actually hit the php 8 nightly build on travis. Nevertheless, I think working with attributes at this early stage can be very helpful:
Since Symfony 5.2 will stabilize together with php 8, I'm marking this PR as ready now. The current implementation of the PR works find with the alpha2 of php 8. If the PR gets merged, I'll monitor the php 8 build on Travis and adjust the implementation if necessary. |
b6c0e65
to
f0978de
Compare
Thank you @derrabus. |
This PR was merged into the 5.2-dev branch. Discussion ---------- Added missing changelog entries | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | #37545 #37474 | License | MIT | Doc PR | N/A I've added the missing changelog entries for the two tickets above. Commits ------- 14642fb Added missing changelog entries.
We've just added Named parameters makes this very intuitive to migrate 👍 Note, there is no |
@TomasVotruba Thank you, I have updated the PR description. The first two iterations of the attribute syntax required me to introduce a dedicated attribute class, so this might have been a leftover from that time. |
This PR was squashed before being merged into the 5.x branch. Discussion ---------- [Routing] Document the Route attribute This PR documents the new `#[Route]` attribute that was introduced with symfony/symfony#37474 and thus closes #14188. For the routing configuration via Doctrine Annotations as well as PHP attributes, we use the very same class, `Symfony\Component\Routing\Annotation\Route`. This means that both mechanisms are equally powerful. Right now, there's nothing you can do with `@Route` that is impossible with `#[Route]` and vice versa. The main difference is that you don't need an external library for attributes because they're a native language feature. Because of that, I'd like to shift the general recommendation to use annotation towards attributes and recommend annotations as the fallback for projects that need to remain compatible with PHP 7. Right now, the `@Route` annotation is used throughout the documentation in two different ways: 1. Demonstrate how to use the annotation In this case, I've added an additional code block that shows the same configuration for attributes. I've consistently placed the attribute block before the annotation block. 2. Give context about the route configuration. When a code block illustrates the logic inside a controller action and the annotation is only used to provide context, I think it would create too much noise to show the attribute _and_ the annotation way. In `routing.rst`, I have replaced the annotation with an equal attribute in such cases. I think it would be a good idea to do the same with the rest of the documentation, but I'd like to discuss this with you before I change all those spots. Commits ------- 4ca794e [Routing] Document the Route attribute
…ration of routing/serializer without doctrine/annotations (derrabus) This PR was merged into the 5.2-dev branch. Discussion ---------- [FrameworkBundle] Allow to use attribute-based configuration of routing/serializer without doctrine/annotations | Q | A | ------------- | --- | Branch? | 5.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | N/A | License | MIT | Doc PR | N/A Follows #37474, #38525 Currently, we need `doctrine/annotations` to be installed in order to configure routing and serializer via PHP attributes. Given that for both components we can already replace Doctrine Annotations completely, I'd like to get rid of that limitation. Commits ------- e5492e2 [FrameworkBundle] Configure PHP Attributes without doctrine/annotations.
I was wondering if we can make use of attributes as a replacement for Doctrine Annotations for configuring routes.
I have modified the existing
AnnotationClassLoader
so that it merges attributes and annotations. This way, an application could transparently switch from annotations to attributes. Moreover, theAnnotationClassLoader
does not require an annotation reader anymore.Since the chosen syntax
#[…]
is treated as a comment by php 7, I was able to use the existing annotation class, which makes the migration even easier.Example with Doctrine Annotations
Same example with the proposed Attribute