今天在 Laravel News 網站無心間看到「Laravel Zero」,主要被這個 Slogan 吸引住了。php
像寫 Laravel 代碼那樣優雅地寫 console/command-line application;並且比 Laravel 和 Lumen 簡單,極易上手。laravel
下面咱們開始寫個簡單的 demo 入手:bash
// 1. 建立 notes 項目腳手架
composer create-project --prefer-dist laravel-zero/laravel-zero notes
// 重命名
php application app:rename notes
// 建立新的命令
php notes make:command AddCommand
複製代碼
在項目路徑下 app/Commands/AddCommand.php,app
具體代碼簡單,直接看代碼:composer
<?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)。ui
個人出發點是:當咱們在開會時,若是臨時有消息和通知,就能夠不須要任何釘釘客戶端,利用「命令行」把內容推送到工做羣裏,效果會比較「極客」👍👍👍。
一樣的,新建一個 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 的理解~~~