「3步」發佈 Laravel 插件 (二)

延續上一篇「12步」製做 Laravel 插件 (一),今天咱們就來完成後續的步驟:寫好的插件部署到「Packagist」上。php

具體只須要如下「3」個步驟:android

1. 註冊 Packagist 帳號

What is Packagist? Packagist is the default Composer package repository. It lets you find packages and lets Composer know where to get the code from. You can use Composer to manage your project or libraries' dependencies.laravel

You can find the packagist.org source on GitHub.git

摘錄:https://packagist.org/aboutgithub

因爲插件代碼選擇託管到 Github 上,因此建議直接使用 Github 帳號登陸。bootstrap

註冊成功後,就能夠拿到用戶名和 API Token,用於關聯 Github 插件代碼。bash

2. 提交代碼到 GitHub

新建 Github 項目,最好與插件同名: 微信

在 Laravel 項目同級路徑下 packages 放着就是咱們的插件代碼「fanly/msgrobot」markdown

提交代碼:app

-l

提交代碼後,在 Github 項目 setting 中增長「Packagist」服務

這裏就是填入「Packagist」的用戶名和 API Token。

3. 發佈插件

只要在 Github 項目中關聯了Packagist 服務,就能夠在 Packagist 後臺「submit」該插件

輸入插件 git 地址,就能夠提交了;

此時,若是沒對代碼進行 tag 控制,獲得插件是「dev-master」版本的。因此須要對插件進行版本設置。

在「git push」代碼後,繼續

這樣咱們就發佈了0.0.1版本的插件了。

測試

有了插件,咱們天然須要測試,看看運行效果了。

咱們選擇利用 Lumen 項目試試,安裝插件

在 bootstrap/app.php 引入註冊插件 ServiceProvider:

$app->register(Fanly\Msgrobot\FanlyMsgrobotServiceProvider::class);
複製代碼

咱們寫一個 test,試試效果,先建立獨立跳轉 ActionCard 類型消息,而後給已建立的機器人對應的「access_token」釘釘羣推送此消息

// text
        $text = new Text('hello fanly/msgrobot package');
        Msgrobot::accessToken('cb36a3c3cab1242b94516d026a02d909f1611ec048d89c93cb3e1132f08b4e')
            ->message($text)
            ->send();

        // link
        $link = new Link([
            'text' => 'link text',
            'title' => 'link title',
            'picUrl' => 'http://f.hiphotos.baidu.com/image/pic/item/503d269759ee3d6db032f61b48166d224e4ade6e.jpg',
            'messageUrl' => 'http://f.hiphotos.baidu.com/image/pic/item/503d269759ee3d6db032f61b48166d224e4ade6e.jpg'
        ]);
        Msgrobot::accessToken('cb36a3c3cab1242b94516d026a02d909f1611ec048d89c93cb3e1132f08b4e')
            ->message($link)
            ->send();


        // markdown
        $md = new Markdown([
            'title' => 'link text',
            'text' => "#### 杭州天氣 @156xxxx8827\n > 9度,西北風1級,空氣良89,相對溫度73%\n\n> ![screenshot](http://image.jpg)\n> ###### 10點20分發布 [天氣](http://www.thinkpage.cn/) \n"
        ]);

        Msgrobot::accessToken('cb36a3c3cab1242b94516d026a02d909f1611ec048d89c93cb3e1132f08b4e')
            ->message($md)
            ->send();

        // Single ActionCard
        $sac = new SingleActionCard([
            'title' => 'link title',
            'text' => '![screenshot](@lADOpwk3K80C0M0FoA) ### 喬布斯 20 年前想打造的蘋果咖啡廳 Apple Store 的設計正從原來滿滿的科技感走向生活化,而其生活化的走向其實能夠追溯到 20 年前蘋果一個創建咖啡館的計劃',
            'hideAvatar' => 0,
            'btnOrientation' => 0,
            'singleTitle' => '閱讀原文',
            'singleURL' => 'http://f.hiphotos.baidu.com/image/pic/item/503d269759ee3d6db032f61b48166d224e4ade6e.jpg'
        ]);

        Msgrobot::accessToken('cb36a3c3cab1242b94516d026a02d909f1611ec048d89c93cb3e1132f08b4e')
            ->message($sac)
            ->send();

        // More ActionCard
        $btns = [
            new ActionCardBtn([
                'title' => '內容不錯',
                'actionURL' => 'http://f.hiphotos.baidu.com/image/pic/item/503d269759ee3d6db032f61b48166d224e4ade6e.jpg'
            ]),
            new ActionCardBtn([
                'title' => '不感興趣',
                'actionURL' => 'http://f.hiphotos.baidu.com/image/pic/item/503d269759ee3d6db032f61b48166d224e4ade6e.jpg'
            ])
        ];

        $mac = new MoreActionCard([
            'title' => 'link title',
            'text' => '![screenshot](@lADOpwk3K80C0M0FoA) ### 喬布斯 20 年前想打造的蘋果咖啡廳 Apple Store 的設計正從原來滿滿的科技感走向生活化,而其生活化的走向其實能夠追溯到 20 年前蘋果一個創建咖啡館的計劃',
            'hideAvatar' => 0,
            'btnOrientation' => 0,
            'btns' => $btns
        ]);

        Msgrobot::accessToken('cb36a3c3cab1242b94516d026a02d909f1611ec048d89c93cb3e1132f08b4e')
            ->message($mac)
            ->send();

        // FeedCard
        $links = [
            new FeedCardLink([
                'title' => "時代的火車向前開",
                "messageURL" => "https://mp.weixin.qq.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI",
                "picURL" => "http://f.hiphotos.baidu.com/image/pic/item/503d269759ee3d6db032f61b48166d224e4ade6e.jpg"
            ]),
            new FeedCardLink([
                'title' => "時代的火車向前開",
                "messageURL" => "https://mp.weixin.qq.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI",
                "picURL" => "http://f.hiphotos.baidu.com/image/pic/item/503d269759ee3d6db032f61b48166d224e4ade6e.jpg"
            ])
        ];

        $fc = new FeedCard($links);

        Msgrobot::accessToken('cb36a3c3cab1242b94516d026a02d909f1611ec048d89c93cb3e1132f08b4e')
            ->message($fc)
            ->send();
複製代碼

總結

經過對這兩篇文章的梳理和學習,相信咱們本身對如何製做 Laravel 插件有了比較直觀的瞭解了,至於插件代碼的質量和 Laravel 技術的理解的深度,有賴於繼續專研。

--此插件--更多做爲學習使用

最後,今天是大年初六,祝你們今年六六六!!!

參考

1. 微信 SDK for Laravel, 基於 overtrue/wechat:https://github.com/overtrue/laravel-wechat

2. 本文章插件地址:https://packagist.org/packages/fanly/msgrobot

3. 本文章插件源代碼:https://github.com/fanly/msgrobot

相關文章
相關標籤/搜索