laravel version: 5.5.*

  • 创建一个命令
php artisan  make:command  CustomCmd
  • 编写命令需要处理的逻辑
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class CustomCmd extends Command
{
    /**
     * 定义命令的名称.
     *
     * @var string
     */
    protected $signature = 'custom:hello';

    /**
     * 定义命令的简单描述.
     *
     * @var string
     */
    protected $description = 'A Test Command';

    /**
     * 创建本命令对象
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 定义命令执行的内容
     *
     * @return mixed
     */
    public function handle()
    {
        echo 'hello world';
    }
}
  • 注册命令
    app/Console/Commands/Kernel.php 文件中修改 $commands 属性
protected $commands = [
    \App\Console\Commands::class,
];
laravel 如何自定义一个artisan命令
show result

相关文章: