Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Command: Autowiring & Interactive Questions

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $12.00

With a Subscription, click any sentence in the script to jump to that part of the video!

Login Subscribe

Last chapter team! Let's do this!

Ok, what if we need a service from inside our command? For example, let's say that we want to use MixRepository to print out a vinyl mix recommendation. How can we do that?

Well, we're inside of a service and we need access to another service, which means we need... the dreaded dependency injection. Kidding - not dreaded, easy with autowiring!

Add public function __construct() with private MixRepository $mixRepository to create and set that property all at once.

... lines 1 - 4
use App\Service\MixRepository;
... lines 6 - 17
class TalkToMeCommand extends Command
{
public function __construct(
private MixRepository $mixRepository
)
{
... line 24
}
... lines 26 - 55
}

Though, if you hover over __construct(), it says:

Missing parent constructor call.

To fix this, call parent::__construct():

... lines 1 - 4
use App\Service\MixRepository;
... lines 6 - 17
class TalkToMeCommand extends Command
{
public function __construct(
private MixRepository $mixRepository
)
{
parent::__construct();
}
... lines 26 - 55
}

This is a super rare situation where the base class has a constructor that we need to call. In fact, this is the only situation I can think of in Symfony like this... so not normally something you need to worry about.

Interactive Questions

Down here, let's output a mix recommendation... but make it even cooler by first asking the user if they want this recommendation.

We can ask interactive questions by leveraging the $io object. I'll say if ($io->confirm('Do you want a mix recommendation?')):

... lines 1 - 17
class TalkToMeCommand extends Command
{
... lines 20 - 34
protected function execute(InputInterface $input, OutputInterface $output): int
{
... lines 37 - 45
$io->success($message);
if ($io->confirm('Do you want a mix recommendation?')) {
... lines 49 - 51
}
... lines 53 - 54
}
}

This will ask that question, and if the user answers "yes", return true. The $io object is full of cool stuff like this, including asking multiple choice questions, and auto-completing answers. Heck, we can even build a progress bar!

Inside the if, get all of the mixes with $mixes = $this->mixRepository->findAll(). Then... we need just a bit of ugly code - $mix = $mixes[array_rand($mixes)] - to get a random mix.

Print the mix with one more $io method $io->note() passing I recommend the mix and then pop in $mix['title']:

... lines 1 - 17
class TalkToMeCommand extends Command
{
... lines 20 - 34
protected function execute(InputInterface $input, OutputInterface $output): int
{
... lines 37 - 45
$io->success($message);
if ($io->confirm('Do you want a mix recommendation?')) {
$mixes = $this->mixRepository->findAll();
$mix = $mixes[array_rand($mixes)];
$io->note('I recommend the mix: ' . $mix['title']);
}
... lines 53 - 54
}
}

And... done! By the way, notice this return Command::SUCCESS? That controls the exit code of your command, so you'll always want to have Command::SUCCESS at the bottom of your command. If there was an error, you could return Command::ERROR.

Okay, let's try this! Head over to your terminal and run:

php bin/console app:talk-to-me --yell

We get the output... and then we get:

Do you want a mix recommendation?

Why, yes we do! And what an excellent recommendation!

All right, team! We did it! We finished - what I think is - the most important Symfony tutorial of all time! No matter what you need to build in Symfony, the concepts we've just learned will be the foundation of doing it.

For example, if you need to add a custom function or filter to Twig, no problem! You do this by creating a Twig extension class... and you can use MakerBundle to generate this for you or build it by hand. It's very similar to creating a custom console command: in both cases, you're building something to "hook into" part of Symfony.

So, to create a Twig extension, you would create a new PHP class, make it implement whatever interface or base class that Twig extensions need (the documentation will tell you that)... and then you just fill in the logic... which I won't show here.

That's it! Behind the scenes, your Twig extension would automatically be seen as a service, and autoconfiguration would make sure it's integrated into Twig... exactly like the console command.

In the next course, we'll put our new superpowers to work by adding a database to our app so that we can load real, dynamic data. And if you have any real, dynamic questions, we are here for you, as always, down in the comment section.

All right, friends. Thanks so much for coding with me and we'll see you next time.

Leave a comment!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=8.1",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "knplabs/knp-time-bundle": "^1.18", // v1.19.0
        "symfony/asset": "6.1.*", // v6.1.0-RC1
        "symfony/console": "6.1.*", // v6.1.0-RC1
        "symfony/dotenv": "6.1.*", // v6.1.0-RC1
        "symfony/flex": "^2", // v2.1.8
        "symfony/framework-bundle": "6.1.*", // v6.1.0-RC1
        "symfony/http-client": "6.1.*", // v6.1.0-RC1
        "symfony/monolog-bundle": "^3.0", // v3.8.0
        "symfony/runtime": "6.1.*", // v6.1.0-RC1
        "symfony/twig-bundle": "6.1.*", // v6.1.0-RC1
        "symfony/ux-turbo": "^2.0", // v2.1.1
        "symfony/webpack-encore-bundle": "^1.13", // v1.14.1
        "symfony/yaml": "6.1.*", // v6.1.0-RC1
        "twig/extra-bundle": "^2.12|^3.0", // v3.4.0
        "twig/twig": "^2.12|^3.0" // v3.4.0
    },
    "require-dev": {
        "symfony/debug-bundle": "6.1.*", // v6.1.0-RC1
        "symfony/maker-bundle": "^1.41", // v1.42.0
        "symfony/stopwatch": "6.1.*", // v6.1.0-RC1
        "symfony/web-profiler-bundle": "6.1.*" // v6.1.0-RC1
    }
}