use*_*810 1 command laravel laravel-artisan
我正在尝试自学 Laravel 命令,以便稍后我使用它来安排它们。这是我的内核文件:
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');
}
}
Run Code Online (Sandbox Code Playgroud)
这是 \App\Console\Commands 中的命令文件
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.');
}
}
Run Code Online (Sandbox Code Playgroud)
我想测试 FooCommand 命令。它如何从 shell 调用此命令,以便结果是“测试已触发。”?
小智 8
手动运行你的命令:php artisan command:name。
删除你的fire函数,你可以处理这个内部handle函数。
在内核类中修复您的日程安排功能
class Kernel extends ConsoleKernel
{
....
protected function schedule(Schedule $schedule)
{
$schedule->command('command:name')
->hourly();
}
}
Run Code Online (Sandbox Code Playgroud)
要配置您的日程安排,请阅读:https : //laravel.com/docs/5.4/scheduling
| 归档时间: |
|
| 查看次数: |
9439 次 |
| 最近记录: |