基于 Laravel Route 的 ThinkSNS+ Component

发布时间:2020-08-07 07:24:55 作者:ThinkSNS官方
来源:网络 阅读:340

这里是传送门:

《ThinkSNS+ 基于 Laravel master 分支,从 1 到 0,再到 0.1【ThinkSNS+研发日记系列一】》

《基于 Laravel 开发 ThinkSNS+ 中前端的抉择(webpack/Vue)踩坑日记【ThinkSNS+研发日记系列二】》

在前面,我介绍了拓展类型,分别有 plus-compnent 和 plus-plugin 两个,这里重点讲以下如何实现 plus-component 的。

plus-component 是什么

就如同名字一样,plus 代表的是 ThinlSNS+ 程序,用 - 分割 后面的 component 就是「包」或者我们理解成应用。在这里的「应用」指的是通过实现 API 或者 web 的功能。所以产生了这个类型。

但是 plus-component 不只是应用,也可以是简单的功能拓展,例如medz/plus-storage-quniu就是拓展的七牛云储存。

composer 插件的建立

既然涉及到路由,最开始的想法,其实是 /routes 目录下生成文件,包的路由文件复制到这里来。后来,发现了问题不足。
最后想到,plus-component 的实现,不一定是基于路由的应用,也有可能是简单的拓展。我们看下中间插件的接口类:

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

    /**
     * 应用安装.
     *
     * @param Closure $next
     *
     * @author Seven Du <shiweidu@outlook.com>
     * @homepage http://medz.cn
     */
    public function install(Closure $next);

    /**
     * 应用升级.
     *
     * @param Closure $next
     *
     * @author Seven Du <shiweidu@outlook.com>
     * @homepage http://medz.cn
     */
    public function update(Closure $next);

    /**
     * 应用卸载.
     *
     * @param Closure $next
     *
     * @author Seven Du <shiweidu@outlook.com>
     * @homepage http://medz.cn
     */
    public function uninstall(Closure $next);

    /**
     * 静态资源.
     *
     * @return string 静态资源目录
     *
     * @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 成了非必需项。

转而,拥有了三个 hook 方法 install、update 和 uninstall 方法,这三个分别对应的是安装,升级,卸载。
而设计中,plus-component 中间插件会在 Laravel 的 /config/component.php 中增加如下配置:

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

中间插件的 composer.json 配置

其实很简单,看到上面添加到 /config/component.php 的代码了, installer 项哪里来的呢?看下 包的配置:

{
    "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 插件执行的时候读取这个额外的配置,并写入到 Laravel 的配置文件中。

install/update/uninstall

在 ThinkSNS+ 中有 php artisan component [install|update|unstall] vendor/name 这样一个命令,主要是用作 包的安装,升级,卸载。
实际运行如下:

php artisan component install medz/plus-component-example
通过这样的方式安装包,而这个命令会读取 /config/component.php 的配置,从而得到 installer ,这样,在运行不同的参数的时候后,调用 install,uodate,uninstall 等 需求 hook 达到目的。

router

在最上面的接口类中你也看到了,有一个 router 方法,这个方法返回类型有两个 void|string,所以, void 代表没有路由,string 就表示包路由文件的绝对地址。

在 php artisan component 命令执行的时候,对应的添加 /config/component_routes.php 里面的配置。
在 /app/Providers/RouteServiceProvider.php 中如下:

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

可能你会误会,为什么只是 include 进来呢?是的,这里的代码其实是参照 Route::group 来的,而在包里面的文件,可以正常的使用 Route::* 进行路由配置。

resource

既然可以基于路由,那就必然会存在静态资源的问题,在接口类中也有这样的规定:

 /**
     * 静态资源.
     *
     * @return string 静态资源目录
     *
     * @author Seven Du <shiweidu@outlook.com>
     * @homepage http://medz.cn
     */
    public function resource();

这里返回在包中静态资源存储的目录,执行安装或者升级命令的时候复制到 /public/vendor/name 目录下来达到静态资源发布的功能。

更高级的封装

这里其实是只模式封装,在 ThinkSNS+ 的 php artisan component 其实还有一个 --link 参数,做什么用的?其实不难理解,就是吧静态资源由原来的复制变成创建软链。这在开发过程中很有用。

下期预告:下一篇文章,会简单的讲以下 ThinkSNS+ 自封装的命令实现。

开源代码仓库:

GitHub:https://github.com/zhiyicx/thinksns-plus(点击星,每日关注开发动态。)

推荐阅读:
  1. 关于 Overtrue 的拼音库 overtrue/pinyin 为何 travis 为 error
  2. ThinkSNS+ 是如何计算字符显示长度的

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

laravel 软件架构 前端开发

上一篇:没执行过 rm -rf /* 的开发不是好运维

下一篇:Linux下共享VG改变活动状态

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》