Skip to content

Instantly share code, notes, and snippets.

@St0iK
Forked from weaverryan/FooController.php
Created April 17, 2017 11:33
Show Gist options
  • Save St0iK/4ccd9ab1ba05c73b156a99c9bcbab78f to your computer and use it in GitHub Desktop.
Save St0iK/4ccd9ab1ba05c73b156a99c9bcbab78f to your computer and use it in GitHub Desktop.
Autowiring controller args
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Psr\Log\LoggerInterface;
/**
* This class is now a service, so you can use normal __construct DI if you want!
*/
class FooController extends Controller
{
/**
* @Route("/cool")
*
* In the controller, type-hint an arg and Symfony will pass you the matching service
*/
public function doCoolStuffAction(LoggerInterface $logger)
{
$logger->info('Huh, that was easy');
// even though your controller is now a service, you can still use
// $this->container->get('some_other_service');
// if you want to, just like before
}
}
services:
_defaults:
autowire: true
# auto-tags services based on their class whenever it makes sense
autoconfigure: true
# registers all classes found in these directories
AppBundle\:
resource: '../../src/AppBundle/{Controller,Service,OtherDir}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment