这里是传送门:

《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(点击星,每日关注开发动态。)

基于 Laravel Route 的 ThinkSNS+ Component的更多相关文章

  1. 【社交系统ThinkSNS+研发日记三】基于 Laravel Route 的 ThinkSNS+ Component

    [社交系统ThinkSNS+研发日记系列] 一.<ThinkSNS+ 基于 Laravel master 分支,从 1 到 0,再到 0.1> 二.<基于 Laravel 开发 Th ...

  2. ThinkSNS+ 基于 Laravel master 分支,从 1 到 0,再到 0.1

    什么是 ThinkSNS+ 09 年,由北京的团队开发了 ThinkSNS 涉足社交开源行业.这么多年累计不少客户.2014-2016,两年都在维护和开发之前基于 TP 的 ThinkSNS , 慢慢 ...

  3. 基于 Laravel、Vue.js开发的全新社交系统----ThinkSNS+

    什么是ThinkSNS+ ThinkSNS(简称TS)始于2008年,一款全平台综合性社交系统,为国内外大中小企业和创业者提供社会化软件研发及技术解决方案,目前最新版本为ThinkSNS+.新的产品名 ...

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

    在上一篇文章< ThinkSNS+基于Laravel master分支,从1到 0,再到0.1>,简单的介绍了 社群系统ThinkSNS+ ,这里分享在开发过程中,前端选择的心理活动. L ...

  5. 基于 Laravel 开发 ThinkSNS+ 中前端的抉择(webpack/Vue)踩坑日记

    在上一篇文章< ThinkSNS+基于Laravel master分支,从1到 0,再到0.1>,简单的介绍了 ThinkSNS+ ,这里分享在开发过程中,前端选择的心理活动. Larav ...

  6. 【转】基于laravel制作APP接口(API)

    这篇文章主要介绍了基于laravel制作APP接口(API)的相关资料,需要的朋友可以参考下 前期准备 前言,为什么做以及要做个啥本人姓小名白,不折不扣编程届小白一名,但是自从大一那会儿接触到编程这件 ...

  7. 基于laravel框架构建最小内容管理系统

    校园失物招领平台开发 --基于laravel框架构建最小内容管理系统 摘要 ​ 针对目前大学校园人口密度大.人群活动频繁.师生学习生活等物品容易遗失的基本现状,在分析传统失物招领过程中的工作效率低下. ...

  8. [开源] LaravelPlus - 基于 Laravel 魔改,为方便实际业务使用 - 开发中

    目的 为了减少重复 CURD 和新项目的配置麻烦等问题,(就是为了骗星星:LaravelPlus )如: 现有的 infyomlabs/laravel-generator CODE 生成工具虽然好用, ...

  9. 基于Laravel+Swoole开发智能家居后端

    基于Laravel+Swoole开发智能家居后端 在上一篇<Laravel如何优雅的使用Swoole>中我已经大概谈到了Laravel结合Swoole的用法. 今天,我参与的智能家居项目基 ...

随机推荐

  1. 【Leetcode-easy】Valid Parentheses

    思路:读取字符串中的每一个字符,并与栈顶元素进行比较,如果匹配则栈顶元素出栈,否则进栈.直到全部元素都出栈,否则该括号字符串不合法.需要注意细节. public boolean isValid(Str ...

  2. jQuery+CSS3动画相册特效

    在线演示 本地下载

  3. java开发环境搭建-1

    安卓开发所需软件: JDK Eclipse Android-Sdk ADT 其中jdk的下载和安装,详细见http://www.cnblogs.com/zhuxiaohui/p/3620685.htm ...

  4. smokeping 微信报警配置

    1. 准备alert脚本,用来调用微信脚本 #!/bin/bash alertname=$ target=$ losspattern=$ rtt=$ smokename="hq_to_idc ...

  5. jsp参数传递

    jsp参数传递 jsp中四种传递参数的方法 1.form表单 2.request.setAttribute();和request.getAttribute(); 3.超链接:<a herf=&q ...

  6. Stop logging "internal dummy connection" in Apache

    Apache 2.x keeps child processes alive by creating internal connections which appear in the log file ...

  7. mongdb启动问题

    问题:Detected unclean shutdown - /data/db/mongod.lock is not empty. old lock file:/data/db/mongod.lock ...

  8. 设计四个线程,其中两个线程每次对j加1,另外两个线程每次对j减1

    public class ManyThreads2 { private int j = 0; public synchronized void inc() { j++; System.out.prin ...

  9. uC/OS-II源码分析(六)

    μC/OS-Ⅱ总是运行进入就绪态任务中优先级最高的那一个.确定哪个任务优先级最高, 下面该哪个任务运行了的工作是由调度器(Scheduler)完成的.任务级的调度是由函数 OSSched()完成的.中 ...

  10. ambari快速安装hadoop

    资源下载http://www.cnblogs.com/bfmq/p/6027202.html 大家都知道hadoop包含很多的组件,虽然很多都是下载后解压简单配置下就可以用的,但是还是耐不住我是一个懒 ...