有很多方法可以做到这一点。
一个选项是将文件路径作为命令参数传递,然后使用基本的file_get_contents()函数读取该文件。
class YourCommand extends Command {
public function fire() {
dd(file_get_contents($this->argument('path'));
}
protected function getArguments()
{
return [['path', InputArgument::REQUIRED, "File path"]];
}
}
您也可以利用Laravel的Filesystem库,并建立本地存储(见https://laravel.com/docs/5.1/filesystem了解详细信息),并把此文件在存储/应用文件夹:
class YourCommand extends Command {
public function fire() {
dd(Storage::get($this->argument('path')));
}
protected function getArguments()
{
return [['path', InputArgument::REQUIRED, "File path"]];
}
}
如果您想要避免提供文件路径,最简单的方法是从STDIN流中读取文件:
class YourCommand extends Command {
public function fire() {
dd(file_get_contents('php://stdin');
}
}
你会只需要运行如下命令:
php artisan yourcommand < file.json