laravel 如何自定义一个artisan命令

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 16:57   1305   0

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,
];
7942449-999a1ea8fd5092f7.png
show result
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP