推荐一个命令行应用开发工具——Laravel Zero

1,540 阅读2分钟

今天在 Laravel News 网站无意间看到「Laravel Zero」,主要被这个 Slogan 吸引住了。

像写 Laravel 代码那样优雅地写 console/command-line application;而且比 Laravel 和 Lumen 简单,极易上手。

下面我们开始写个简单的 demo 入手:

创建项目

// 1. 创建 notes 项目脚手架
composer create-project --prefer-dist laravel-zero/laravel-zero notes

// 重命名
php application app:rename notes

// 创建新的命令
php notes make:command AddCommand

add note

在项目路径下 app/Commands/AddCommand.php,

  • 修改 $signature 值,命令值为:add,一个参数名为:note; 具体参考:laravel.com/docs/5.6/ar…
  • 修改 $description,描述每个 Command 含义;
  • 最后在 handle() 函数,写入执行 command 的代码

具体代码简单,直接看代码:

<?php

namespace App\Commands;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Facades\Storage;
use LaravelZero\Framework\Commands\Command;

class AddCommand extends Command
{
    /**
     * The signature of the command.
     *
     * @var string
     */
    protected $signature = 'add 
                            {note : the note description}';

    /**
     * The description of the command.
     *
     * @var string
     */
    protected $description = '增加一条记录';

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle(): void
    {
         $this->info('增加一条记录');

         Storage::append('notes', $this->argument('note'));

         $this->notify('Notes', '增加了一条内容');
    }

    /**
	 * Define the command's schedule.
	 *
	 * @param  \Illuminate\Console\Scheduling\Schedule $schedule
	 *
	 * @return void
	 */
	public function schedule(Schedule $schedule): void
	{
		// $schedule->command(static::class)->everyMinute();
	}
}

利用php notes命令,可以看到一些常用指令,同时可以看到已经增加了一个 command:

测试一下,看效果:

发送的 note 信息都保存到 storage/notes 文件里:

发钉钉消息

简简单单几条代码,就可以做一个命令行应用,确实好用;

接下来,我们开始加点料。结合之前的钉钉发消息插件 (fanly/msgrobot)。

我的出发点是:当我们在开会时,如果临时有消息和通知,就可以不需要任何钉钉客户端,利用「命令行」把内容推送到工作群里,效果会比较「极客」👍👍👍。

同样的,新建一个 DingDingCommand:

安装 fanly/msgrobot 插件:

看代码:

<?php

namespace App\Commands;

use Fanly\Msgrobot\Dingtalk\Messages\Text;
use Fanly\Msgrobot\Facades\Msgrobot;
use Illuminate\Console\Scheduling\Schedule;
use LaravelZero\Framework\Commands\Command;

class DingDingCommand extends Command
{
    /**
     * The signature of the command.
     *
     * @var string
     */
    protected $signature = 'ding {text : DingDing message}';

    /**
     * The description of the command.
     *
     * @var string
     */
    protected $description = '给群发消息';

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle(): void
    {
        $this->info('发送消息');

        // text message
        $text = new Text($this->argument('text'));
        Msgrobot::accessToken('cb36a3c3cab1242b94516d026a02d909f1611ec048d89c93cb3e1132f08b4e')
            ->message($text)
            ->send();

        $this->notify('钉钉', '发送一条消息到群');

    }

    /**
	 * Define the command's schedule.
	 *
	 * @param  \Illuminate\Console\Scheduling\Schedule $schedule
	 *
	 * @return void
	 */
	public function schedule(Schedule $schedule): void
	{
		// $schedule->command(static::class)->everyMinute();
	}
}

试试发一个消息:

因为「Laravel Zero」只用了 Laravel 核心的几个库,所以在使用第三方插件时,有可能会报错,如找不到 Class log 之类的:

这时候是缺什么补什么即可:

composer require illuminate/log

同时,还需要补上 logging.php 配置文件

*注:*这里推荐利用 Laravel Zero 官网提供的方法,一步到位:

php <your-app-name> app:install log

最后我们看看执行效果:

打包

app notes app:build notes

如果打包时,提示需要设置 readonly,直接按照提示修改配置文件即可

打包完后,就可以直接使用该钉钉发送工具了:

总结

因为时间关系,没有尝试使用 Laravel Zero 的其它功能,我相信只要看看官网的介绍,使用起来应该游刃有余,总之一句话:够简单,够轻量级

如果你觉得 Laravel Zero 还不错,你也可以试试哦,也可以加深对 Laravel 的理解~~~

参考