简体   繁体   中英

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

eg

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?

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM