laravel 如何自定义一个artisan命令

laravel version: 5.5.*

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

namespace AppConsoleCommands;

use IlluminateConsoleCommand;

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 = [
    AppConsoleCommands::class,
];
7942449-999a1ea8fd5092f7.png
show result
原文地址:https://www.cnblogs.com/liaohui5/p/10581588.html