-
-
Save St0iK/4ccd9ab1ba05c73b156a99c9bcbab78f to your computer and use it in GitHub Desktop.
Autowiring controller args
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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