Laravel里的自定义命令使用技巧

前言

作为一个PHPer,但是好久不写PHP了,今天就回过来聊聊Laravel的一个使用技巧。

Laravel里可以自定义命令:
https://laravel.com/docs/5.8/artisan#generating-commands

也可以方便的撰写定时任务:
https://laravel.com/docs/5.8/scheduling

但是不要忘了,定时任务里是可以直接执行你创建的自定义命令的喔。

虽然从5.5的文档开始,就专门建了目录来说这个事情,但是作为一个老用户,有时候往往会忽略了这个组合用法。

运用场景

我们现在开发的一个产品是有很多缓存到redis里的数据的,都是用定时任务来执行。然而,偶尔可能会需要即时更新一下,这个时候用自定义命令就很合适,进入服务器终端,然后命令一敲,缓存立马刷新了,就很方便。

操作步骤

对于不惧怕看英文文档的同学,建议直接查看官方文档就行。
这里我就做个小Demo,手把手走走过场。

1. 创建自定义命令

php artisan make:command HelloWorld

然后就会自动创建一个自定义命令的文件:app/Console/Commands/HelloWorld.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class HelloWorld 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()
{
//
}
}

然后我们把相应的代码写进去

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class HelloWorld extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'hello-world';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Hello World.';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
echo "Hello World!";
}
}

最后我们在命令行输入
php artisan hello-world
就会看到命令行打印出了Hello World!

2. 创建定时任务

打开app/Console/Kernel.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Console\Commands\HelloWorld; // use刚才自定义的命令

class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
HelloWorld::class // 把自定义命令注册到这里
];

/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('hello-world')->everyMinute();
}

/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');

require base_path('routes/console.php');
}
}

然后我们在命令行执行:
php artisan schedule:run
这个命令相当于是直接执行命令,不管定时任务的周期,一般用于代码开发调试的时候。

输出如下,根据你的环境可能会不一样,但意思都是相同的:
Running scheduled command: '/usr/local/Cellar/[email protected]/7.2.13/bin/php' 'artisan' hello-world > '/dev/null' 2>&1

好了,这样就把Laravel的定时任务和自定义命令结合起来了。希望在你的开发中能够用得上。

加载评论框需要科学上网