https://github.com/tomasvotruba/symfony-legacy-controller-autowire
https://github.com/tomasvotruba/symfony-legacy-controller-autowire
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/tomasvotruba/symfony-legacy-controller-autowire
- Owner: TomasVotruba
- License: mit
- Created: 2022-01-17T10:20:13.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-01-17T10:49:45.000Z (over 3 years ago)
- Last Synced: 2025-01-12T11:49:34.262Z (6 months ago)
- Language: PHP
- Homepage: https://tomasvotruba.com/blog/2016/03/10/autowired-controllers-as-services-for-lazy-people/
- Size: 190 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Controller Autowire for Symfony 2.8
* 
This bundle does only 2 things:
- **1. registers controllers as services**
- **2. enables constructor autowiring for them**Still wondering **why use controller as services**? Check [this](http://richardmiller.co.uk/2011/04/15/symfony2-controller-as-service) and
[this](http://php-and-symfony.matthiasnoback.nl/2014/06/how-to-create-framework-independent-controllers/) article.## Install
```bash
composer require tomasvotruba/symfony-legacy-controller-autowire
```Add bundle to `AppKernel.php`:
```php
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = [
new TomasVotruba\SymfonyLegacyControllerAutowire\SymplifyControllerAutowireBundle(),
// ...
];
}
}
```## Usage
```php
class SomeController
{
private $someClass;public function __construct(SomeClass $someClass)
{
$this->someClass = $someClass;
}
}
```## Used to FrameworkBundle's controller? Use helpers traits!
Inspired by [pull](https://github.com/symfony/symfony/pull/18193) [requests](https://github.com/symfony/symfony/pull/20493) to Symfony and setter injection that are currently on-hold, **here are the traits you can use right now**:
```php
use TomasVotruba\SymfonyLegacyControllerAutowire\Controller\Routing\ControllerAwareTrait;final class SomeController
{
use ControllerAwareTrait;public function someAction()
{
$productRepository = $this->getDoctrine()->getRepository(Product::class);
// ...return $this->redirectToRoute('my_route');
}
}
```
### Do you prefer only traits you use?
```php
use TomasVotruba\SymfonyLegacyControllerAutowire\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 :)
## Contributing
Send [issue](https://github.com/Symplify/Symplify/issues) or [pull-request](https://github.com/Symplify/Symplify/pulls) to main repository.