有很多方法可以做到這一點。
一個選項是將文件路徑作爲命令參數傳遞,然後使用基本的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