zoukankan      html  css  js  c++  java
  • thinkphp6-4

    、ThinkRoute->dispatch()  路由

    public function dispatch(Request $request, $withRoute = null) { //设置传入的Request对象到当前对象的属性上 $this->request = $request; //同上 这个是设置host $this->host = $this->request->host(true); //执行Route init 方法 初始化 $this->init(); //判断的withRoute是否为真 if ($withRoute) { //执行传入过来的匿名函数,加载路由 $withRoute(); //官方注释的是检测url路由,我这里姑且认为是路由分发吧 //check返回的是think outeDispatch 路由调度基础类对象 $dispatch = $this->check(); } else { //调用think outedispatchUrl类 //将当前的url地址传入进去,进行默认的url解析 $dispatch = $this->url($this->path()); } //执行Dispatch对象中的init方法 //这里用于绑定控制器和方法以及路由后置操作,例如:中间件、绑定模型数据 $dispatch->init($this->app); //执行路由调度。并返回一个Response对象 return $this->app->middleware->pipeline('route') ->send($request) ->then(function () use ($dispatch) { return $dispatch->run(); }); }

    6、ThinkRoute->init()  初始化

    protected function init() { //合并默认配置以及读取到的route.php配置 $this->config = array_merge($this->config, $this->app->config->get('route')); //判断路由中间件是否存储,如果存在则调用middleware类中的import方法 //注册route中间件 if (!empty($this->config['middleware'])) { $this->app->middleware->import($this->config['middleware'], 'route'); } //是否延迟解析 $this->lazy($this->config['url_lazy_route']); //读取到的 是否合并路由配置项赋值到类变量mergeRuleRegex中 $this->mergeRuleRegex = $this->config['route_rule_merge']; //获取配置:是否删除url最后的斜线 $this->removeSlash = $this->config['remove_slash']; //是否去除url最后的斜线 $this->group->removeSlash($this->removeSlash); }

    7、ThinkRouteDispatch->init()

    1
    2
    3
    4
    5
    6
    public function init(App $app)
    {
        $this->app = $app;
        // 执行路由后置操作
        $this->doRouteAfter();
    }

    8、ThinkRouteDispatch->run()

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    public function run(): Response
    {
        //判断$this->rule路由规则是否为RuleItem类的实例
        //判断当前请求方法,是不是OPTIONS以及
        //判断当前路由规则是否为自动注册的OPTIONS路由
        if ($this->rule instanceof RuleItem && $this->request->method() == 'OPTIONS' && $this->rule->isAutoOptions()) {
            //获取当前的路由列表
            $rules $this->rule->getRouter()->getRule($this->rule->getRule());
            $allow = [];
            foreach ($rules as $item) {
                //这里是循环把所有路由全部转成大写
                $allow[] = strtoupper($item->getMethod());
            }
            //创建并返回一个Response对象,调用create静态方法
            return Response::create('''html', 204)->header(['Allow' => implode(', '$allow)]);
        }
        //如果上面的不匹配则调用当前Dispatch类中的exec方法
        //实例化控制器以及方法
        $data $this->exec();
        //最后动态的返回一个Response对象。xml、json等等
        return $this->autoResponse($data);
    }
  • 相关阅读:
    Java的代码风格
    哪些你容易忽略的C语言基础知识
    Java基础学习笔记第二章
    Java代码性能优化总结
    Java并发编程(2):线程中断(含代码)
    C语言代码训练(一)
    数控G代码编程详解大全
    PLC编程算法
    博客转移到新地址
    一些吐槽
  • 原文地址:https://www.cnblogs.com/huaobin/p/14910224.html
Copyright © 2011-2022 走看看