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

7236ba1ccb8e87d78119753f81089d74.png

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

像写 Laravel 代码那样优雅地写 console 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;具体参考:https://laravel.com/docs/5.6/artisan#defining-input-expectations

修改 $description,描述每个 Command 含义;

最后在 handle() 函数,写入执行 command 的代码

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

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:

4b05a6544ceb67bde1ec0b12ecfec648.png

测试一下,看效果:

4c7e3f24f14c87fc5967ede075409b0a.png

bad771d93406144ef0762895f6e95bed.png

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

e0e7309b0ffdc839a92ef373a3ed82a3.png

发钉钉消息

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

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

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

同样的,新建一个 DingDingCommand:

d68602f2b58a97905ec92f2d994923ac.png

安装 fanly/msgrobot 插件:

71512132ac4ab7e05e1ac4987d9ef0b6.png

看代码:

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('cb36a3c3ca***')

->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();

}

}

2ac117f1d05d85f296d796ad4c6ca73b.png

试试发一个消息:

01630af56fd3726ab3a532ba0070f76c.png

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

f748336893bf0bd29f2a74c963155316.png

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

composer require illuminate/log

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

41f5ba006a25c483e5d5109a93afaee9.png

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

php app:install log

e69989c069a444e68ae12d1f8756725d.png

最后我们看看执行效果:

a470d9190306396d3ebec057c16acba4.png

f0477b20c49be23670b8481b91a975d1.png

打包

app notes app:build notes

c6b5b9f15dd8867f34b28abe800082f6.png

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

4c46f90a441c58dbf0269e24d2a026d5.png

9463443e9a3fb735075a588009f64e6a.png

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

b31460c4a36ddc232ea3eaf10a550ff6.png

总结

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

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

参考

官网 http://laravel-zero.com/#/

Laravel Zero 5.6 Is Now Available https://medium.com/@nunomaduro/laravel-zero-5-6-is-now-available-3c83a6dc267f

「完」

coding01 期待您继续关注

8d9fc0fc8d579c471d1271ba033e26f5.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值