平时开发过程中经常碰到写个数据处理脚本,统计脚本的情况,都需要在Laravel项目的环境下执行,而官方提供的artisan命令却不支持直接执行php脚本文件,这里记录下自己做的一个Command,用来解决这类问题。//1.使用命令行创建文件
php artisan make:command RunFile
//2.将一下内容粘贴到RunFile文件中
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputArgument;
class RunFile extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'run';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Run php file with system env support.';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$file = $this->argument('php_file');
if (is_file($file)) {
include $file;
} else {
$this->error("file '{$file}' not found!");
}
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array(
array('php_file', InputArgument::REQUIRED, 'The php file to run with Laputa.'),
);
}
}
//3.测试运行php artisan run [完整的路径]
php artisan run /home/vagrant/Code/library_site/site/app/Utils/Tools.php
|