0a5998d996
This PR was merged into the 3.3-dev branch.
Discussion
----------
[FrameworkBundle] Add new "controller.service_arguments" tag to inject services into actions
| Q | A
| ------------- | ---
| Branch? | master
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | (no test yet)
| Fixed tickets | -
| License | MIT
| Doc PR | -
Talking with @simensen and @weaverryan, we wondered if we could leverage the `ArgumentResolver` mechanism to make it inject services on demand, using e.g. autowiring.
```php
class PostController
{
public function indexAction(Request $request, PostRepository $postRepository)
{
// PostRepository comes from the container
$postRepository->findAll(); // ...
}
}
```
This PR achieves that, using a new "controller.service_arguments" tag. Typically:
```yaml
services:
AppBundle\Controller\PostController:
autowire: true
tags:
- name: controller.service_arguments
```
It also supports with explicit wiring (thus doesn't necessarily require autowiring if you don't want to use it):
```yaml
services:
AppBundle\Controller\PostController:
tags:
- name: controller.service_arguments
action: fooAction
argument: logger
id: my_logger
```
~~The attached diff is bigger than strictly required for now, until #21770 is merged.~~
Todo:
- [x] rebase on top of #21770 when merged
- [x] add tests
- [x] add cleaning pass to remove empty service locators
Commits
-------
|
||
---|---|---|
.. | ||
Bundle | ||
CacheClearer | ||
CacheWarmer | ||
Config | ||
Controller | ||
ControllerMetadata | ||
DataCollector | ||
Debug | ||
DependencyInjection | ||
Event | ||
EventListener | ||
Exception | ||
Fragment | ||
HttpCache | ||
Log | ||
Profiler | ||
Tests | ||
.gitignore | ||
CHANGELOG.md | ||
Client.php | ||
composer.json | ||
HttpKernel.php | ||
HttpKernelInterface.php | ||
Kernel.php | ||
KernelEvents.php | ||
KernelInterface.php | ||
LICENSE | ||
phpunit.xml.dist | ||
README.md | ||
TerminableInterface.php | ||
UriSigner.php |
HttpKernel Component
The HttpKernel component provides a structured process for converting a Request into a Response by making use of the EventDispatcher component. It's flexible enough to create a full-stack framework (Symfony), a micro-framework (Silex) or an advanced CMS system (Drupal).