Skip to content

Conversation

@nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Aug 21, 2025

Q A
Branch? 7.4
Bug fix? no
New feature? yes
Deprecations? no
Issues #60946
License MIT

Currently, controllers have to be put in predetermined directories for their route attributes to be parsed.

This PR enables auto-registration of controllers based on their service definition, independently of where they're located in the app - which means they could be put anywhere now.

config/routes.yaml before:

controllers:
    resource:
        path: ../src/Controller/
        namespace: App\Controller
    type: attribute

config/routes.yaml after:

controllers:
    resource: attributes
    type: tagged_services

Recipe update at symfony/recipes#1448

@nicolas-grekas nicolas-grekas marked this pull request as ready for review August 21, 2025 16:05
@carsonbot carsonbot added this to the 7.4 milestone Aug 21, 2025
@symfony symfony deleted a comment from carsonbot Aug 21, 2025
@nicolas-grekas nicolas-grekas force-pushed the routing-service-loader branch from efeefc6 to 687deb3 Compare August 21, 2025 16:10
Copy link
Member

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

routing.controller is a new tag. How will it be added to the class definition?

Edit: the tag is added to classes with #[Route] or #[AsController] attribute.

@MatTheCat
Copy link
Contributor

Seems this PR would fix #60946 (ping @Nayte91)?

@stof
Copy link
Member

stof commented Aug 21, 2025

@GromNaN the PR includes autoconfiguration rules to add the tag.

@nicolas-grekas nicolas-grekas changed the title [FrameworkBundle] Auto-register #[Route] attributes found on services tagged with routing.controller [Routing][FrameworkBundle] Auto-register routes from attributes found on controller services Aug 22, 2025
@carsonbot carsonbot changed the title [Routing][FrameworkBundle] Auto-register routes from attributes found on controller services [FrameworkBundle][Routing] Auto-register routes from attributes found on controller services Aug 22, 2025
@nicolas-grekas nicolas-grekas force-pushed the routing-service-loader branch from 687deb3 to 219bea5 Compare August 22, 2025 07:43
@nicolas-grekas
Copy link
Member Author

PR updated, thanks for the reviews.

controllers:
    resource: attributes
    type: tagged_services

@nicolas-grekas nicolas-grekas force-pushed the routing-service-loader branch from 219bea5 to 0ce304f Compare August 22, 2025 07:44
Copy link
Member

@yceruto yceruto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super !

@fabpot
Copy link
Member

fabpot commented Aug 22, 2025

Thank you @nicolas-grekas.

@fabpot fabpot merged commit 12fb3d1 into symfony:7.4 Aug 22, 2025
10 of 12 checks passed
*
* @author Nicolas Grekas <[email protected]>
*/
class AttributeServicesLoader extends Loader
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest making it final or @final. I don't see any need to support inheritance on this class (there is no useful place to hook into anyway)

@nicolas-grekas nicolas-grekas deleted the routing-service-loader branch August 22, 2025 16:06
nicolas-grekas added a commit that referenced this pull request Aug 25, 2025
…esLoader final (xabbuh)

This PR was merged into the 7.4 branch.

Discussion
----------

[Routing] make RoutingControllerPass and AttributeServicesLoader final

| Q             | A
| ------------- | ---
| Branch?       | 7.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Issues        | see #61492 (comment)
| License       | MIT

Commits
-------

9986139 make RoutingControllerPass and AttributeServicesLoader final
nicolas-grekas added a commit that referenced this pull request Aug 26, 2025
… of known DI tags (xabbuh)

This PR was merged into the 7.4 branch.

Discussion
----------

[DependencyInjection] add routing.controller to the list of known DI tags

| Q             | A
| ------------- | ---
| Branch?       | 7.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Issues        |
| License       | MIT

re-reading #61492 I wonder if we don't have to add the new tag to this list

Commits
-------

593ee8f add routing.controller to the list of known DI tags
This was referenced Oct 27, 2025
fabpot added a commit that referenced this pull request Nov 5, 2025
…ler services (nicolas-grekas)

This PR was merged into the 7.4 branch.

Discussion
----------

[Routing] Simplify importing routes defined on controller services

| Q             | A
| ------------- | ---
| Branch?       | 7.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Issues        | -
| License       | MIT

Fine-tuning #61492

Before:

```yaml
controllers:
    resource: attributes
    type: tagged_services
```

After:

```yaml
controllers:
    resource: routing.controllers
```

And when using `MicroKernelTrait` (the default recipe), there's nothing to configure to have routes loaded from `#[Route]` attributes:

```yaml
#config/routes.yaml
# the file can be just empty from any config, and only have a comment saying what it's for
```

```php
// config/routes.php
return Routes::config([
]);
```

The way to opt-out from this `routing.controllers` import would be to override the `Kernel::configureRoutes()` method.

Commits
-------

13e25ca [Routing] Simplify importing routes defined on controller services
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants