This bundle does only 2 things. But does them well:
- 1. registers controllers as services and
- 2. enables constructor autowiring for them
Still wondering why use controller as services? Check this and this article.
composer require symplify/controller-autowire
Add bundle to AppKernel.php
:
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = [
new Symplify\ControllerAutowire\SymplifyControllerAutowireBundle(),
// ...
];
}
}
class SomeController
{
private $someClass;
public function __construct(SomeClass $someClass)
{
$this->someClass = $someClass;
}
}
Inspired by pull requests to Symfony and setter injection that are currently on-hold, here are the traits you can use right now:
use Symplify\ControllerAutowire\Controller\Routing\ControllerAwareTrait;
final class SomeController
{
use ControllerAwareTrait;
public function someAction()
{
$productRepository = $this->getDoctrine()->getRepository(Product::class);
// ...
return $this->redirectToRoute('my_route');
}
}
use Symplify\ControllerAutowire\Controller\Routing\ControllerRoutingTrait;
final class SomeController
{
use ControllerRoutingTrait;
public function someAction()
{
return $this->redirectToRoute('my_route');
}
}
Just type Controller*Trait
in your IDE to autocomplete any of these traits.
That's all :)
Send issue or pull-request to main repository.