Thinkphp5.1源码阅读

主要流程

 

1 publicindex.php

require __DIR__ . '/../thinkphp/start.php';

2 hinkphpstart.php

require __DIR__ . '/base.php';
// 执行应用并响应
Container::get('app', [defined('APP_PATH') ? APP_PATH : ''])
    ->run()
    ->send();

  2.1 hinkphpase.php

require __DIR__ . '/library/think/Loader.php';
// 注册自动加载
Loader::register();
// 注册核心类到容器
Container::getInstance()->bind([
    'app'                   => App::class, //App::class ≈ string(9) "thinkApp"
//...
    'view'                  => View::class,
//...
]);

3 hinkphplibrary hinkApp.php


public function run()
{
// 初始化应用
$this->initialize(); //主要加载配置,助手函数
// 执行调度
$data = $dispatch->run(); //$dispatch = object(think outedispatchUrl)
//...
$response = Response::create(); // hinkphplibrary hinkResponse.php
//...
return $response;

  3.1 thinkphplibrary hink outedispatchUrl.php

public function run()
{
    // 解析默认的URL规则
    $url    = str_replace($this->param['depr'], '|', $this->dispatch);
    $result = $this->parseUrl($url);
    return (new Module($result))->run();
}

      3.1.1 thinkphplibrary hink outedispatchModule.php

public function run()
{
// 实例化控制器
    $instance = $this->app->controller( //thinkphplibrary	hinkApp.php
//...
  $call = [$instance, $action]; //$instance = object(appusercontrollerHome)
//...
  return Container::getInstance()->invokeMethod($call, $vars);

    3.2  hinkphplibrary hinkResponse.php

public static function create($data = '', $type = '', $code = 200, array $header = [], $options = [])
    {
     //...
if (class_exists($class)) { return new $class($data, $code, $header, $options); } else { return new static($data, $code, $header, $options); } }

  4

//发送数据到客户端
public function send()
    {
//...
echo $data;

控制器中 $this->fetch()

thinkphplibrary hinkController.php

public function __construct()
    {
        $this->request = Container::get('request');
        $this->app     = Container::get('app');
        $this->view    = Container::get('view')->init(
            $this->app['config']->pull('template'),
            $this->app['config']->get('view_replace_str')
        );
protected function fetch($template = '', $vars = [], $replace = [], $config = [])
    {
        return $this->view->fetch($template, $vars, $replace, $config);
    }

thinkphplibrary hinkView.php

public function init($engine = [], $replace = [])
    {
        // 初始化模板引擎
        $this->engine($engine); 
public function engine($options = [])
{
  
$type = !empty($options['type']) ? $options['type'] : 'Think';   $class = false !== strpos($type, '\') ? $type : '\think\view\driver\' . ucfirst($type);
  $this->engine = new $class($options); //$this->engine = thinkphplibrary hinkviewdriverThink.php
  return $this;
}
原文地址:https://www.cnblogs.com/8000cabbage/p/7508271.html