您好,欢迎访问一九零五行业门户网

解析TP5框架从入口到输出界面的加载流程

thinkphp框架教程栏目将给大家介绍分析thinkphp5框架从入口到输出界面的加载流程,希望对需要的朋友有所帮助!
分析thinkphp5框架从入口到输出界面的加载流程安装thinkphp怎么安装,我就不细说了,官方文档-安装thinkphp说的很全了,可以通过composer、git或者直接去thinkphp官网下载zip包,我安装的版本是5.0.24
测试运行下载安装完毕后,如果项目是下载目录是你本地服务器的项目根目录下,可以直接在浏览器输入地址http://localhost/thinkphp5/public/,就可以进入到thinkphp5的默认欢迎页,如下图所示,这就说明thinkphp5已经安装成功
除了上面的这个方式的地址运行,我们也可以通过apache或者nginx配置虚拟主机实现项目的访问,有兴趣的可以网上查看具体教程,然后配置虚拟主机进行访问。
下面进入正题,我们来逐步分析thinkphp5的执行流程……
入口文件(publicindex.php)打开public\index.php文件后,我们可以看到,入口文件原始代码如下
// [ 应用入口文件 ]// 定义应用目录define('app_path', __dir__ . '/../application/');// 加载框架引导文件require __dir__ . '/../thinkphp/start.php';
入口文件代码很简洁,就两行代码,作用分别为
define('app_path', __dir__ . '/../application/');定义应用目录的常量app_pathrequire __dir__ . '/../thinkphp/start.php';加载框架引导文件除了上面的这两个作用外,我们还可以额外在入口文件中,定义我们自己的常量,例如添加一行代码define('public_path', __dir__ .'/../public');定义public目录的常量以及一些预处理等
加载框架引导文件(thinkphpstart.php)同样的,进入thinkphp\start.php文件后,我们可以知道,代码并不多
namespace think;// thinkphp 引导文件// 1. 加载基础文件require __dir__ . '/base.php';// 2. 执行应用app::run()->send();
从这简短的两行代码,我们可以看到,主要左右有两个
require __dir__ . '/base.php';加载基础文件app::run()->send();执行应用下面两个大点,将具体介绍这两个左右都做了什么
加载基础文件(thinkphpbase.php)我们继续打开thinkphp\base.php文件,发现这个文件终于不再像前两个文件那样,只有两行代码了……
define('think_version', '5.0.24');define('think_start_time', microtime(true));define('think_start_mem', memory_get_usage());define('ext', '.php');define('ds', directory_separator);defined('think_path') or define('think_path', __dir__ . ds);define('lib_path', think_path . 'library' . ds);define('core_path', lib_path . 'think' . ds);define('trait_path', lib_path . 'traits' . ds);defined('app_path') or define('app_path', dirname($_server['script_filename']) . ds);defined('root_path') or define('root_path', dirname(realpath(app_path)) . ds);defined('extend_path') or define('extend_path', root_path . 'extend' . ds);defined('vendor_path') or define('vendor_path', root_path . 'vendor' . ds);defined('runtime_path') or define('runtime_path', root_path . 'runtime' . ds);defined('log_path') or define('log_path', runtime_path . 'log' . ds);defined('cache_path') or define('cache_path', runtime_path . 'cache' . ds);defined('temp_path') or define('temp_path', runtime_path . 'temp' . ds);defined('conf_path') or define('conf_path', app_path); // 配置文件目录defined('conf_ext') or define('conf_ext', ext); // 配置文件后缀defined('env_prefix') or define('env_prefix', 'php_'); // 环境变量的配置前缀// 环境常量define('is_cli', php_sapi == 'cli' ? true : false);define('is_win', strpos(php_os, 'win') !== false);// 载入loader类require core_path . 'loader.php';// 加载环境变量配置文件if (is_file(root_path . '.env')) {    $env = parse_ini_file(root_path . '.env', true);    foreach ($env as $key => $val) {        $name = env_prefix . strtoupper($key);        if (is_array($val)) {            foreach ($val as $k => $v) {                $item = $name . '_' . strtoupper($k);                putenv($item=$v);            }        } else {            putenv($name=$val);        }    }}// 注册自动加载\think\loader::register();// 注册错误和异常处理机制\think\error::register();// 加载惯例配置文件\think\config::set(include think_path . 'convention' . ext);
仔细一看,发现代码虽然有六十多行,但是,代码的作用却显而易见,作用主要有以下六点
使用define('', '')函数定义了很多个系统常量,外加两个环境常量引入loader类(thinkphplibrarythinkloader.php),供后续使用加载环境变量配置文件(环境变量配置文件名为.env,这个文件不一定存在,都是在实际开发过程中根据需要加上去的)调用\think\loader::register()注册自动加载机制
注册系统自动加载composer自动加载支持注册命名空间定义加载类库映射文件,存在于runtime缓存目录下classmap.php自动加载extend目录调用\think\error::register()注册异常和错误处理机制加载惯例配置文件(thinkphpconvention.php)执行应用(thinkphplibrarythinkapp.php)下的run方法为了方便,这个run方法的代码虽然有点长,但是我还是选择把整个方法贴出来,别打我哈
/** * 执行应用程序 * @access public * @param  request $request 请求对象 * @return response * @throws exception */public static function run(request $request = null){    $request = is_null($request) ? request::instance() : $request;    try {        $config = self::initcommon();        // 模块/控制器绑定        if (defined('bind_module')) {            bind_module && route::bind(bind_module);        } elseif ($config['auto_bind_module']) {            // 入口自动绑定            $name = pathinfo($request->basefile(), pathinfo_filename);            if ($name && 'index' != $name && is_dir(app_path . $name)) {                route::bind($name);            }        }        $request->filter($config['default_filter']);        // 默认语言        lang::range($config['default_lang']);        // 开启多语言机制 检测当前语言        $config['lang_switch_on'] && lang::detect();        $request->langset(lang::range());        // 加载系统语言包        lang::load([            think_path . 'lang' . ds . $request->langset() . ext,            app_path . 'lang' . ds . $request->langset() . ext,        ]);        // 监听 app_dispatch        hook::listen('app_dispatch', self::$dispatch);        // 获取应用调度信息        $dispatch = self::$dispatch;        // 未设置调度信息则进行 url 路由检测        if (empty($dispatch)) {            $dispatch = self::routecheck($request, $config);        }        // 记录当前调度信息        $request->dispatch($dispatch);        // 记录路由和请求信息        if (self::$debug) {            log::record('[ route ] ' . var_export($dispatch, true), 'info');            log::record('[ header ] ' . var_export($request->header(), true), 'info');            log::record('[ param ] ' . var_export($request->param(), true), 'info');        }        // 监听 app_begin        hook::listen('app_begin', $dispatch);        // 请求缓存检查        $request->cache(            $config['request_cache'],            $config['request_cache_expire'],            $config['request_cache_except']        );        $data = self::exec($dispatch, $config);    } catch (httpresponseexception $exception) {        $data = $exception->getresponse();    }    // 清空类的实例化    loader::clearinstance();    // 输出数据到客户端    if ($data instanceof response) {        $response = $data;    } elseif (!is_null($data)) {        // 默认自动识别响应输出类型        $type = $request->isajax() ?        config::get('default_ajax_return') :        config::get('default_return_type');        $response = response::create($data, $type);    } else {        $response = response::create();    }    // 监听 app_end    hook::listen('app_end', $response);    return $response;}
这大概90行的代码,具体做了什么呢,结合注释分析,主要有以下几步的功能
第一步:处理变量$request,保证有效有用不为null第二步:self::initcommon()调用当前控制器中的initcommon()方法,负责初始化应用,并返回配置信息
loader::addnamespace(self::$namespace, app_path);注册命名空间self::init()调用本类的init()方法初始化应用
加载各种配置文件加载行为扩展文件加载公共文件加载语言包应用调试模式相关处理加载额外文件,通过配置项extra_file_list的值去加载相关文件date_default_timezone_set($config['default_timezone']);设置系统时区调用hook::listen('app_init');监听app_init标签的行为第三步:判断是否进行模块或者控制器的绑定第四步:系统语言设置和加载第五步:self::routecheck($request, $config)加载当前控制器的routecheck()方法进行路由检测
先进行路由地址配置检测,先读取缓存路由,不存在再导入路由文件配置无路由配置,直接解析模块/控制器/操作返回module模块信息(模块名、控制器名和操作方法名)第六步:开启调试模式下,记录路由和请求信息的日志第七步:self::exec($dispatch, $config)调用控制器中的exec()方法执行调用分发
根据用户请求类型进行分发处理,这里是module模块类型调用self::module()执行模块,进行模块部署和初始化,获取和设置当前控制器名和操作名第八步:清空类的实例化,并输出相应格式的数据到客户端,即用户看到的输出界面总结本文大概解析了thinkphp5的基础执行流程,有说的不到位的,也不用给我说了,因为我也不会补上去的,就是这么皮;但是如果是说错的呢,麻烦指出来,我肯定会加以改正的,就这么耿直。对了,如果觉得对你有帮助,点个赞再走呗,感谢!
以上就是解析tp5框架从入口到输出界面的加载流程的详细内容。
其它类似信息

推荐信息