Symfony2 console output without OutputInterface

I am trying to print some information on the console in the Symfony Console command. Regularly you do something like this:

protected function execute(InputInterface $input, OutputInterface $output) { $name = $input->getArgument('name'); if ($name) { $text = 'Hello '.$name; } else { $text = 'Hello'; } if ($input->getOption('yell')) { $text = strtoupper($text); } $output->writeln($text); } 

Full Sample Code - Symfony Documentation

Unfortunately, I cannot access OutputInterface . Is it possible to print a message on the console?

Unfortunately, I cannot pass OutputInterface to the class where I want to print some output.

+4
source share
2 answers

Understanding the issue of efficient debugging, you can always print debugging messages using echo or var_dump

If you plan to use a command without a Symfony application with global debugging messages, here is a way to do it.

Symfony offers 3 different OutputInterface s

Debug to file

By doing this, whenever you call $output->writeln() on your command, it will write a new line in /path/to/debug/file.log

 use Symfony\Component\Console\Output\StreamOutput; use Symfony\Component\Console\Input\ArrayInput; use Acme\FooBundle\Command\MyCommand; $params = array(); $input = new ArrayInput($params); $file = '/path/to/debug/file.log'; $handle = fopen($file, 'w+'); $output = new StreamOutput($handle); $command = new MyCommand; $command->run($input, $output); fclose($handle); 

Debugging in the console

This is the same process, except that you use ConsoleOutput instead

 use Symfony\Component\Console\Output\ConsoleOutput; use Symfony\Component\Console\Input\ArrayInput; use Acme\FooBundle\Command\MyCommand; $params = array(); $input = new ArrayInput($params); $output = new ConsoleOutput(); $command = new MyCommand; $command->run($input, $output); 

No debugging

No message will be printed

 use Symfony\Component\Console\Output\NullOutput; use Symfony\Component\Console\Input\ArrayInput; use Acme\FooBundle\Command\MyCommand; $params = array(); $input = new ArrayInput($params); $output = new NullOutput(); $command = new MyCommand; $command->run($input, $output); 
+8
source

Source: https://habr.com/ru/post/1497079/


All Articles