Skip to content
Go to file
Latest commit 47a5f5e Jan 31, 2018 History
call the parent constructor
5 contributors

Users who have contributed to this file

@fprochazka @mrtnzlml @h4kuna @galvani @DavidKrupicka
138 lines (96 sloc) 3.87 KB


This extension is here to provide integration of Symfony Console into Nette Framework.


The best way to install Kdyby/Console is using Composer:

$ composer require kdyby/console

You can enable the extension using your neon config.

    console: Kdyby\Console\DI\ConsoleExtension

Minimal configuration

This extension creates new configuration section console, the absolute minimal configuration might look like this


The url key specifies reference url that allows you to generate urls using LinkGenerator in CLI (which is not possible otherwise).

Running the console

It is suggested, that you create a bin/console file, with the following contents

#!/usr/bin/env php
/** @var \Nette\DI\Container $container */
$container = require __DIR__ . '/../app/bootstrap.php';
$console = $container->getByType(\Symfony\Component\Console\Application::class);

Make sure the console script is executable by running chmod +x bin/console.

And test it by running php bin/console (but just bin/console should work too), it should list all available commands.

Writing commands

Commands are like controllers, but for Symfony Console. Example command might look like this

namespace App\Console;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class SendNewslettersCommand extends Command

    /** @var \Models\NewsletterSender */
    private $newsletterSender;

    public function __construct(NewsletterSender $newsletterSender)
        $this->newsletterSender = $newsletterSender;

    protected function configure(): void
            ->setDescription('Sends the newsletter');

    protected function execute(InputInterface $input, OutputInterface $output): int
        try {
            $output->writeLn('Newsletter sent');
            return 0; // zero return code means everything is ok

        } catch (\Nette\Mail\SmtpException $e) {
            $output->writeLn('<error>' . $e->getMessage() . '</error>');
            return 1; // non-zero return code means error

The configure method is to name the command and specify arguments and options. They have a lot of options and you can read about them in Symfony Documentation.

When the command is executed, the execute method is called with two arguments. First one is command input, which contains all the parsed arguments and options. The second one is command output which should be used to provide feedback to the developer which ran the command.

Best practise is to return an exit code which specifies if the command ran successfully and can be read by other applications when executed.

Registering commands

To add a command, register it as a service with tag kdyby.console.command

        class: App\Console\SendNewslettersCommand
        tags: [kdyby.console.command]

To add a helper, register it as a service with tag kdyby.console.helper

        class: App\Console\FooHelper
        tags: [kdyby.console.helper]

Shorter configuration

If you want to register all your commands and don't want to write tags to all of them, you can use the Nette Decorator extension

        tags: [kdyby.console.command]

    - App\Console\SendNewslettersCommand

Nette will add the tag to all the command services automatically and they will get picked by Kdyby/Console and registered as commands.