Thinkphp5.1源码阅读

主要流程

 

1 \public\index.php

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

2 \thinkphp\start.php

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

  2.1 \thinkphp\base.php

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

3 \thinkphp\library\think\App.php


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

  3.1 thinkphp\library\think\route\dispatch\Url.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 thinkphp\library\think\route\dispatch\Module.php

public function run()
{
// 实例化控制器
    $instance = $this->app->controller( //thinkphp\library\think\App.php
//...
  $call = [$instance, $action]; //$instance = object(app\user\controller\Home)
//...
  return Container::getInstance()->invokeMethod($call, $vars);

    3.2 \thinkphp\library\think\Response.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()

thinkphp\library\think\Controller.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);
    }

thinkphp\library\think\View.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 = thinkphp\library\think\view\driver\Think.php
  return $this;
}

 

posted @ 2017-09-12 08:26  xiaobaicaidage  阅读(724)  评论(0编辑  收藏  举报