Download the PHP package pureisle/micro-mvc without Composer
On this page you can find all versions of the php package pureisle/micro-mvc. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download pureisle/micro-mvc
More information about pureisle/micro-mvc
Files in pureisle/micro-mvc
Package micro-mvc
Short Description simple、efficient、 easy to project migration, DMVC+C architecture design, only for PHP 7.0+
License MIT
Homepage https://github.com/pureisle/MicroMVC
Informations about the package micro-mvc
为什么用
- 不过度设计,简单、清晰、好用。经历过国内一线互联网公司众多项目实践,能很好的解决大部分业务研发过程中遇到的团队协作、项目管理、质量和安全等问题;
- 提供最简单基础的 MVC 框架,将性能损耗降到最低。关于性能;
- 支持简单的Lua MVC , 需要 Openresty 。
- 按 Module 进行资源分离,以便对业务进行微服务化隔离或后期的服务便捷迁移。关于Module和迁移;
- 提供简单好用的单元测试框架。关于单元测试;
- 提供便捷的接口参数合法性验证服务,接口合法性调用检测服务,做了基础的防SQL注入、CSRF、XSS等安全防护。关于安全;
- 提供简单好用工具类,如 Mysql 、Curl 、多进程管理(pcntl)、常驻进程任务监控保活等资源的封装、Xhprof 性能优化工具等;
- 根据代码运行的环境自动加载相应的配置文件,方便的切换仿真、生产环境;
- 提供PSR-3规范的日志类,额外提供 log buffer 功能(性能提升) 和 全局日志标记码(一个进程一个标记码,方便定位问题)的功能;
- 所有开发基于 PHP7 环境,未做低版本运行验证和兼容;
框架工具库说明
- ConfigTool 框架统一的配置文件读取类,限定了配置文件的存放路径,可支持 .php 或 .ini 或 自定义文件的配置读取。
- Context 环境变量类,可以使用全局的 Context::$G ,也可以自定义。主要用于层级较深的参数传递、全局变量存储等。
- Curl 发起HTTP协议请求的客户端类。
- MultiCurl 并行批量发起HTTP请求的客户端类。
- Daemon CLI模式下任务执行基类,一般配合 bins/run_daemon.php 或 ProcessManager 使用。
- DaemonMonitor 常驻进程的任务监控类,探测任务文件变化而重启任务、监控任务、超时任务kill、退出的任务重新拉起等。
- Debug 调试类,可以设置调试信息的输出和禁用。生产环境会强制禁用输出调试信息。
- Degrader 降级类,提供三种降级方式:1、按单个Key降级;2、按群组降级;3、按时间范围降级;
- Exception 异常管理基类,强制按静态数组的方式定义异常,统一管理。
- KeyBuilder 强制按一定方式定义和编码KEY值的工具类。
- PHPFunctionParser 针对PHP代码文件,利用 token_get_all 函数进行格式化解析。主要用来做单元测试时,计算测试覆盖率。
- ProcessManager 多进程管理类,可以控制一批任务执行并监控和记录任务执行过程中的状态、资源利用等。
- RunTime 计时器,主要提供了按Key记录运行时间的开始、停止和输出的方法。
- TestSuite 单元测试工具的基类,可以通过 bins/run_test.php 工具来执行单元测试。
- UnitTest 单元测试控制类,主要提供了单元测试控制的功能。
- UserAgentParser UA解析类,主要提供了客户端请求中携带UserAgent的分析方法。
- Validator 变量或参数合法性检验工具。提供了一些常用的检验方法。
- Tools 常用工具类
- retryAgent 执行重试代理,可以方便的执行一段代码的多次重试。
- base64UrlEncode base64UrlDecode 64进制编解码
- getEnv setEnv 框架内比较重要的一个方法,可以设置代码运行环境标识,用以区分和使用不同的配置文件,方便多环境运行。
- iniFuncEnabled 检验ini配置中的函数是否可以使用。
- isCli 检查当前代码是否为CLI模式运行。
- uniqid 生成指定长度的随机码,伪随机。
- 网络相关
- cidrMatch 检查一个IP是否是在一个指定网络内
- getClientIp 获取请求的客户端IP
- getServerIp 获取代码运行环境的IP
- getHostName 获取代码运行环境的hostname
- [资源管理] 一些常用的资源工具
- IniParser INI 格式的配置文件的编码和解码工具。
- Logger 日志类,符合PSR-3标准。额外提供进程内唯一标识码,方便一次性排查出一次请求的所有相关日志。
- Memcached Memcached管理类,进行了简单封装,主要目的是强制统一资源配置文件的部署和格式。
- ControllCache 统一的缓存管理基类,提供了三级缓存,分别为:1、内存缓存;2、本地文件缓存;3、远端缓存(redis、mc)。
- ControllMysql SQL语句封装类,可以不写SQL构建数据库查询,同时提供了较为完善的安全防护。
- PDOManager PDO管理类,强制了资源配置部署和格式;提供了基础方法的封装;
- Redis Redis扩展的简单封装,主要是为了强制统一资源配置文件的部署和格式。
- [设计模式] 一些常用的设计模式工具
- EntityBase 实体类基类,提供参数列表限制和参数校验等基础能力。
- FiniteState 状态机的状态基类,主要是为配合状态机FiniteStateMachine使用。
- FiniteStateMachine 状态机管理类,提供状态注册、管理、运行控制等功能,使用样例见PHPFunctionParser。
- HookManager 钩子管理类,通过配置文件定义钩子,在触发钩子时执行预定义的方法或函数。
- SingletonManager 单例工厂,提供了进程内类的统一管理和复用。可以使用全局定义的单例工厂 SingletonManager::$SINGLETON_POOL,也可以自定义一个单例工厂。
快速开始
其中{project_name}替换成目标项目名,{module_name}替换成目标模块名。
文件目录
文件结构也体现了 "DMVC+C" 的分层思想。
- "D" 层为数据层,对应的文件夹为"Data"。该层主要解决数据结构的封装,对上层屏蔽底层的数据结构、存储工具等细节;
- "M" 层为逻辑层,对应的文件夹为"Models"。该层主要解决业务逻辑的封装,对上层屏蔽业务逻辑的细节;
- "C" 层为控制层,对应的文件夹为"Controllers"。该层主要控制相应URL提供哪些服务,根据自身提供的服务引用Model层提供的服务。该层主要负责:安全检验、登录检验、根据接口的目的调用相应 Model、日志记录等;
- "V" 层为视图层,对应文件夹为"Views"。该层主要提供前端页面渲染的代码,主要为HTML代码,夹杂少量JS、CSS等,或作为目前流行的前后端分离设计的前端入口文件等;
- "+C" 层为缓存层,对应的文件夹为"Cache"。该层主要控制缓存相关逻辑。
- "public" 文件夹主要存放静态资源,如图片、JS代码库、CSS表等;
详细的情况可以参见 Sso Module,有更多的使用样例,包括 Cache、config、Bootstrap.php等
开始使用
MVC框架
-
配置 Web Server 服务器重定向到入口文件。Nginx 样例如下:
- 路由解析规则:域名后第一个用'/'分离的部分为 module 名,最后一部分为 action 名,中间部分解析为 controller。如:
这里要注意,url结尾有没有"/"很关键,结尾有"/"意味着 Action 的值会解析成 index 。
样例项目中,访问 sso 地址为: http://domain.example.com/sso/login
- 每个 module 可以有自己的 Bootstrap.php 在自己的根目录里,在框架初始化时会顺序执行'_init'开头的成员方法。
-
每个 module 有自己的路由插件在 Plugins 文件夹内,可以在 Bootstrap 类中调用 Dispatcher 类的 registerPlugin 方法进行插件注册。 插件包含routerStartup、routerShutdown、dispatchStartup、dispatchShutdown、preResponse几个部分。分别为:
-
每个 Controller 类必须继承 Framework\Models\Controller 。Controller 中的 Action 后缀类成员方法为可以调用的接口。
每个接口可以定义一个对应的参数合法性检验的静态变量,静态变量名的对应规则为: "全大写的接口名_PARAM_RULES"。如 'indexAction' 的参数定义如下: - 每个 Action 若返回结果不为 False ,则会加载相应的 View 视图,视图可以混写 PHP 代码。
在Action内可以调用 $this->assign() 方法注册渲染变量。如:$this->assign(array('text' => 'Hello,world!'));
相应的视图加载规则: Controller名\Action名.phtml。如:
老项目使用MicroMVC框架工具库
其中 exec_function 为要执行的函数, $argv 是参数列表,可以不传或为空。
如何进行仿真环境配置文件加载重定向
- 在 config 下创建 {env_name} 文件夹, {env_name} 名字任意,'pro' 为保留的关键字,视为生产环境标志。文件夹内的配置文件命名同正式的配置文件名即可;
- ConfigTool 加载配置文件时,会依次判断静态变量 $_env 、 $_COOKIE['VISIT_SERVER_ENV'] 和 $_SERVER['VISIT_SERVER_ENV'],如有设置环境名,则启用相应环境的配置文件夹下的同名配置文件。
- \Framework\Libraries\Tools::setEnv(string $env) 可以设置环境名,此时会给 静态变量 $_env 和 setcookie()。
- 样例参考 Sso 下的 config\dev\database.php 和 config\database.php ,分别会在仿真环境和生产环境读取。生产环境下,不会重定向到非生产环境的配置文件,Tools::getEnv() 强制返回生产环境标志,以防止cookie伪造。
如何使用接口认证
-
通过配置文件设置认证方式,安全认证字段主要包括(可以参考Sso\config\api_auth.php):
- app_secret参与的签名验证;需要开启参数use_sign = true 和设置 app_secret 值
- 白名单验证,需要设置 white_ips , 值的格式为: 10.83,10.222.69.0/27,127.0.0.1,10.210.10,10
- 请求时间有效性验证,在app_secret参与验证的基础上增加设置 valid_time值大于0,则会进行时间验证,该值的单位时间为10s
-
签名相关接收的参数为:
- app_key 接口调用方id
- app_sign 接口调用方加密后的签名
- app_time 如果需要时间有效性验证,则会覆盖占用该参数,接口参数定义不要使用这个参数
- app_secret 及 时间验证的签名规则:
- 参数数组增加签证密钥,如:$params['app_secret']=$app_secret,如果需要验证时间,则需增加 $params['app_time']= intval(time() / 10);
- 把参数数组构建为无下标的新数组,如: $tmp = array('param_a=1','param_b=stringxxx','app_secret=xxx')
- 对新数组进行按字母生序排序,如: sort($tmp);
- 使用字符"&"合并排序后的数组生成字符串,如: $params_str = implode('&',$tmp);
- 使用md5获取哈希值,取前6位,至此获得参数的签名字符串,如: $sign = substr(md5($params_str), 0, 6);
如何进行性能优化
-
在想进行代码优化的开始位置执行以下代码:
- 随后在进程结束后会给出查看程序执行细节profile的链接,点击查看即可。
如何使用框架提供的异常处理服务
-
参考Sso\Bootstrap.php 初始化函数:
- 告诉框架自己想要处理的异常 "\Framework\Models\ControllerException" 以及处理该异常的匿名函数即可。
- 这里有一点需要注意,匿名函数可以有返回值告诉框架,这个异常是否处理成功,如果返回 false ,则框架认为未处理成功,会继续抛出这个异常。