简体   繁体   English

在laravel 5中,如何从外部类访问Command类方法?

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

I'm developing a Illuminate\\Console\\Command. 我正在开发一个Illuminate \\ Console \\ Command。 To be run via cli using php artisan. 使用php artisan通过cli运行。 This Command class is using other classes. 此Command类正在使用其他类。 I appreciate the Command->info(), Command->error(), methods... How can I use them in dependencies? 我很欣赏Command-> info(),Command-> error(),方法......我怎样才能在依赖中使用它们?

Until now I'm passing to other classes $this as parameter 到现在为止,我将其作为参数传递给其他类$ this

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"); 我希望方法可以静态访问,如: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 ”来返回“ $ this-> command-> error ”,因为您可以在内核中使用并保存在不同的日志中,如下所示:

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 在app \\ Console \\ Kernel.php中

$schedule->command('mycommand')
            ->everyTenMinutes()
            ->sendOutputTo(storage_path('logs/mycommand.log'))
            ->name('mycommand')
            ->withoutOverlapping();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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