laravel-zero icon indicating copy to clipboard operation
laravel-zero copied to clipboard

Calling info() outside of command

Open BestPluginsWordpress opened this issue 5 years ago • 2 comments

Is it possible to call info() outside the command? Or do I have to pass the command's $this to the other class so I can call info() through it?

BestPluginsWordpress avatar Jan 05 '21 23:01 BestPluginsWordpress

I'm not entirely sure what you mean, do you mean in another class that is called by the command?

The info() method is provided by the underlying Laravel Command class. https://github.com/illuminate/console/blob/master/Concerns/InteractsWithIO.php#L273-L283

owenvoke avatar Jan 06 '21 18:01 owenvoke

Not sure whether this will help, but what I do is pass the instance of the command ($this) to the class I am invoking within the command.

mathewparet avatar Jul 14 '21 07:07 mathewparet

Is it possible to call info() outside the command? Or do I have to pass the command's $this to the other class so I can call info() through it?

You can useIlluminate\Console\Concerns\InteractsWithIO trait in any class to access those output methods. Also, you need to set the output property.

An example,


namespace App\Services;

use Illuminate\Console\Concerns\InteractsWithIO;
use Symfony\Component\Console\Output\ConsoleOutput;

class SomeService
{
    use InteractsWithIO;

    public function __construct()
    {
        $this->output = new ConsoleOutput();
    }

    public function doSomething(string $text)
    {
        $this->line($text);
    }

}

0netwosix avatar Sep 13 '23 22:09 0netwosix

Going to close this as it's a really old issue, and has solutions here. 🤷🏻

owenvoke avatar Sep 14 '23 06:09 owenvoke