Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In laravel 5, how to access to Command class methods from outside classes?

I'm developing a Illuminate\Console\Command. To be run via cli using php artisan. This Command class is using other classes. I appreciate the Command->info(), Command->error(), methods... How can I use them in dependencies?

Until now I'm passing to other classes $this as parameter

e.g.

class MyClass extends Command {
....
    $g = new MyOtherClass($this, $param...);
    $g->find();
....
}

class MyOtherClass {
$command;
....
    public function __construct($command){
        $this->command=$command;
    }
    public function find(){
        if($error)
             $this->command->error($error);
    }
....
}

I wished methods could be accessed statically like: Command::error("some error");

But maybe this is not the intended use?

like image 477
koalaok Avatar asked Nov 09 '22 07:11

koalaok


1 Answers

I suggest you use "echo" to return instead "$this->command->error", because you can use in Kernel and save in a different log, like this:

echo '['.date('Y-m-d H:i:s').'] local.ERROR: '.$error.PHP_EOL; // This way it will be better visible in log viewer.

And in app\Console\Kernel.php

$schedule->command('mycommand')
            ->everyTenMinutes()
            ->sendOutputTo(storage_path('logs/mycommand.log'))
            ->name('mycommand')
            ->withoutOverlapping();
like image 107
RafaelQm Avatar answered Nov 14 '22 23:11

RafaelQm