从控制台调用laravel artisan命令
问题描述:
我想教自己Laravel命令,以便稍后使用它来安排它们。这是我的内核文件:从控制台调用laravel artisan命令
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//
'App\Console\Commands\FooCommand',
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
$schedule->command('App\Console\Commands\FooCommand')->hourly();
}
/**
* Register the Closure based commands for the application.
*
* @return void
*/
protected function commands()
{
require base_path('routes/console.php');
}
}
这是\软件\控制台\命令
namespace App\Console\Commands;
use Illuminate\Console\Command;
class FooCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:name';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//
}
public function fire()
{
$this->info('Test has fired.');
}
}
我想测试FooCommand命令里面的命令文件。它如何从shell调用这个命令,以便结果是“Test has fired。”?
答
手动运行您的命令:php artisan command:name
。
删除你的fire
功能,你可以在里面处理这个handle
函数。
在内核级
class Kernel extends ConsoleKernel
{
....
protected function schedule(Schedule $schedule)
{
$schedule->command('command:name')
->hourly();
}
}
修复你的日程安排功能要配置您的日程安排,请阅读本: https://laravel.com/docs/5.4/scheduling
感谢您的答复。如果我添加另一个命令文件如“SecondCommand”?我怎么称呼它? – user7432810
'schedule:run'命令将执行在'schedule'函数中注册的所有命令。只需添加一个新命令,如'$ schedule-> command('command:name') - > hourly();'schedule'函数内部。 – MarcosRJJunior