日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

基于 Laravel Route 的 ThinkSNS+ Component

發(fā)布時(shí)間:2025/3/20 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于 Laravel Route 的 ThinkSNS+ Component 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

這里是傳送門:

《ThinkSNS+ 基于 Laravel master 分支,從 1 到 0,再到 0.1【ThinkSNS+研發(fā)日記系列一】》

《基于 Laravel 開發(fā) ThinkSNS+ 中前端的抉擇(webpack/Vue)踩坑日記【ThinkSNS+研發(fā)日記系列二】》

?

在前面,我介紹了拓展類型,分別有?plus-compnent?和?plus-plugin?兩個(gè),這里重點(diǎn)講以下如何實(shí)現(xiàn)?plus-component?的。

plus-component?是什么

就如同名字一樣,plus 代表的是 ThinlSNS+ 程序,用?-?分割 后面的?component?就是「包」或者我們理解成應(yīng)用。在這里的「應(yīng)用」指的是通過實(shí)現(xiàn) API 或者 web 的功能。所以產(chǎn)生了這個(gè)類型。

但是?plus-component 不只是應(yīng)用,也可以是簡單的功能拓展,例如medz/plus-storage-quniu就是拓展的七牛云儲存。

composer?插件的建立

既然涉及到路由,最開始的想法,其實(shí)是?/routes?目錄下生成文件,包的路由文件復(fù)制到這里來。后來,發(fā)現(xiàn)了問題不足。
最后想到,plus-component 的實(shí)現(xiàn),不一定是基于路由的應(yīng)用,也有可能是簡單的拓展。我們看下中間插件的接口類:

<?php namespace Zhiyi\Component\Installer\PlusInstallPlugin; use Closure; use Illuminate\Console\Command; use Illuminate\Console\OutputStyle; interface InstallerInterface{public function setCommand(Command $command, OutputStyle $output);/*** Get the component info.** @return void|\Zhiyi\Component\Installer\PlusInstallPlugin\ComponentInfoInterface** @author Seven Du <shiweidu@outlook.com>* @homepage http://medz.cn*/public function getComponentInfo();/*** 應(yīng)用安裝.** @param Closure $next** @author Seven Du <shiweidu@outlook.com>* @homepage http://medz.cn*/public function install(Closure $next);/*** 應(yīng)用升級.** @param Closure $next** @author Seven Du <shiweidu@outlook.com>* @homepage http://medz.cn*/public function update(Closure $next);/*** 應(yīng)用卸載.** @param Closure $next** @author Seven Du <shiweidu@outlook.com>* @homepage http://medz.cn*/public function uninstall(Closure $next);/*** 靜態(tài)資源.** @return string 靜態(tài)資源目錄** @author Seven Du <shiweidu@outlook.com>* @homepage http://medz.cn*/public function resource();/*** 路由配置.** @return string 路由配置文件列表** @author Seven Du <shiweidu@outlook.com>* @homepage http://medz.cn*/public function router();}

其中 router 成了非必需項(xiàng)。

轉(zhuǎn)而,擁有了三個(gè)?hook 方法?install、update?和?uninstall?方法,這三個(gè)分別對應(yīng)的是安裝,升級,卸載。
而設(shè)計(jì)中,plus-component 中間插件會在 Laravel 的?/config/component.php?中增加如下配置:

'medz/plus-component-example' => array ('installed' => false,'installer' => 'Medz\\Component\\ZhiyiPlus\\PlusComponentExample\\Installer\\Installer',),

中間插件的?composer.json?配置

其實(shí)很簡單,看到上面添加到?/config/component.php?的代碼了,?installer?項(xiàng)哪里來的呢?看下包的配置:

{"name": "medz/plus-component-example","type": "plus-component","require": {"zhiyicx/plus-install-plugin": "^1.1"},"autoload": {"psr-4": {"Medz\\Component\\ZhiyiPlus\\PlusComponentExample\\": "src/"}},"extra": {"installer-class": "Medz\\Component\\ZhiyiPlus\\PlusComponentExample\\Installer\\Installer"} }

就是最后的?extra.installer-class?配置的,這里是完整的 class name,這樣,在 composer 插件執(zhí)行的時(shí)候讀取這個(gè)額外的配置,并寫入到 Laravel 的配置文件中。

install/update/uninstall

在?ThinkSNS+ 中有?php artisan component [install|update|unstall] vendor/name?這樣一個(gè)命令,主要是用作 包的安裝,升級,卸載。
實(shí)際運(yùn)行如下:

php artisan component install medz/plus-component-example

通過這樣的方式安裝包,而這個(gè)命令會讀取?/config/component.php?的配置,從而得到 installer ,這樣,在運(yùn)行不同的參數(shù)的時(shí)候后,調(diào)用 install,uodate,uninstall 等 需求 hook 達(dá)到目的。

router

在最上面的接口類中你也看到了,有一個(gè)?router?方法,這個(gè)方法返回類型有兩個(gè) void|string,所以, void 代表沒有路由,string 就表示包路由文件的絕對地址。

在?php artisan component?命令執(zhí)行的時(shí)候,對應(yīng)的添加?/config/component_routes.php?里面的配置。
在?/app/Providers/RouteServiceProvider.php?中如下:

protected function mapVendorRoutes(){$files = config('component_routes', []);foreach ($files as $file) {include_once $file;}}

可能你會誤會,為什么只是?include 進(jìn)來呢?是的,這里的代碼其實(shí)是參照?Route::group?來的,而在包里面的文件,可以正常的使用?Route::*?進(jìn)行路由配置。

resource

既然可以基于路由,那就必然會存在靜態(tài)資源的問題,在接口類中也有這樣的規(guī)定:

/*** 靜態(tài)資源.** @return string 靜態(tài)資源目錄** @author Seven Du <shiweidu@outlook.com>* @homepage http://medz.cn*/public function resource();

這里返回在包中靜態(tài)資源存儲的目錄,執(zhí)行安裝或者升級命令的時(shí)候復(fù)制到?/public/vendor/name?目錄下來達(dá)到靜態(tài)資源發(fā)布的功能。

更高級的封裝

這里其實(shí)是只模式封裝,在?ThinkSNS+ 的?php artisan component?其實(shí)還有一個(gè)?--link?參數(shù),做什么用的?其實(shí)不難理解,就是吧靜態(tài)資源由原來的復(fù)制變成創(chuàng)建軟鏈。這在開發(fā)過程中很有用。

下期預(yù)告:下一篇文章,會簡單的講以下 ThinkSNS+ 自封裝的命令實(shí)現(xiàn)。

開源代碼倉庫:

GitHub:https://github.com/zhiyicx/thinksns-plus(點(diǎn)擊星,每日關(guān)注開發(fā)動態(tài)。)

轉(zhuǎn)載于:https://www.cnblogs.com/thinkSNS/p/thinksns.html

總結(jié)

以上是生活随笔為你收集整理的基于 Laravel Route 的 ThinkSNS+ Component的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。