多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] * * * * * ## 1 全局路由文件源代码(thinkphp/library/think/Route.php的变量与方法名) ~~~ private static $rules = [ 'GET' => [], 'POST' => [], 'PUT' => [], 'DELETE' => [], 'HEAD' => [], '*' => [], ]; private static $rest = [ 'index' => ['GET', '', 'index'], 'create' => ['GET', '/create', 'create'], 'read' => ['GET', '/:id', 'read'], 'edit' => ['GET', '/:id/edit', 'edit'], 'save' => ['POST', '', 'save'], 'update' => ['PUT', '/:id', 'update'], 'delete' => ['DELETE', '/:id', 'delete'], ]; private static $map = []; private static $domain = []; private static $subDomain = ''; private static $pattern = []; private static $bind = []; ~~~ `public static function map($map = '', $route = ''){}` `public static function pattern($name = '', $rule = ''){}` `public static function domain($domain = '', $rule = ''){}` `private static function setting($var, $name = '', $value = ''){}` `public static function bind($type, $bind = ''){}` `public static function register($rule, $route = '', $type = '*', $option = [], $pattern = []){}` `public static function group($name, $routes = [], $type = '*', $option = [], $pattern = []){}` `public static function any($rule, $route = '', $option = [], $pattern = []){}` `public static function get($rule, $route = '', $option = [], $pattern = []){}` `public static function post($rule, $route = '', $option = [], $pattern = []){}` `public static function put($rule, $route = '', $option = [], $pattern = []){}` `public static function delete($rule, $route = '', $option = [], $pattern = []){}` `public static function resource($rule, $route = '', $option = [], $pattern = []){}` `public static function rest($name, $resource = []){}` `public static function getRules($method = ''){}` `public static function checkDomain(){}` `public static function check($url, $depr = '/', $checkDomain = false){}` `private static function checkUrlBind(&$url, &$rules){}` `private static function checkOption($option, $url){}` `private static function checkRule($rule, $route, $url, $pattern, $option){}` `public static function isSsl(){}` `public static function parseUrl($url, $depr = '/'){}` `private static function parseRoute($url, $reverse = false){}` `private static function match($url, $rule, $pattern){}` `private static function parseRule($rule, $route, $pathinfo){}` `private static function parseUrlParams($url, $var){}` ~~~ ## 2 分析 Route.php是框架的路由文件。实现了丰富多样的路由规则定义 1 静态变量 `$rule $rest $map $domain $subDomain $pattern $bind` 用来缓存注册的路由信息。 `3 map() pattern() domain() bind()` 分别用来添加$map $pattern $domain $bind静态变量信息 4 `register()` 添加$rule路由规则信息 5 `group()` 路由按$type分组 6 `any()` 调用register()注册任意风格的路由 7 `get() post() put() delete() resource() rest()` 以Rest方式注册路由 8 `getRules() checkDomain() check()` 用来获取路由规则信息,并对$url进行检测 9 `isSsl()` 检测是否开启SSL协议 10 `parsUrl()` 解析url到模块/控制器/操作/参数 ## 3 总结 Route.php是系统框架的路由实现机制。 在应用启动文件App.php中。解析输入的url到对应的模块/控制器/方法。 是框架运行的核心部分。 可以用来定义url的解析方式,url到模块控制的映射。使用见使用范例的[路由规则注册](http://www.kancloud.cn/zmwtp/tp5/120040)