您的位置:首页 > 编程语言 > PHP开发

yaf 框架路由解析过程

2016-05-31 19:26 671 查看
http://blog.csdn.net/bingqingsuimeng/article/details/20380329

概联总结

$_SERVER['PATH_INFO']  可以理解为第一个文件后面的路径信息 

PATH_INFO'
Contains any client-provided pathname information trailing the actual script filename but preceding the query string, if available. For instance, if the current script was accessed via the URLhttp://www.example.com/php/path_info.php/some/stuff?foo=bar,
then $_SERVER['PATH_INFO'] would
contain/some/stuff.

$_SERVER['REQUEST_URL'] 始终是 nginx中listen监听端口后面的内容  注意document_url是request_url 重写后的内容 location 中的匹配只能匹配 request_url即 端口后面的内容 (包含了问号后面) 

注意很多nginx 等服务器中已经设置成 和 script_name 一样的只(fastcgi.conf中设置fastcgi_param  PATH_INFO          $fastcgi_script_name;)以防止上传恶意文件

REQUEST_URI'
The URI which was given in order to access this page; for instance, '/index.html'.总结REQUEST_URL PHP_SELF SCRIPT_NAME 的区别在于是否包含文件后面的 路径 以及 query_string

SCRIPT_NAME(只包括文件名)--PHP_SELF(包括文件名和路径)--包括文件名,路径以及QUERY参数 而PATH_INFO(需要服务器单独开启,默认不开启)可能为空或者是文件名后面的路径

 注意默认 nginx 不开启PATH_INFO, 防止恶意文件上传到服务器
http://www.cnblogs.com/zcy_soft/archive/2010/10/16/1853239.html
以上都是基于DOCUMENT_ROOT
http://php.net/reserved.variables.server
   1 yaf_application.construct (Yaf_Application::__construct(mixed $config, string $environ = YAF_G(environ_name))) YAF_G 表示配置文件  environ可



    

     

       1.1 调用yaf_config_instance(null, config, environ) (对于odp来讲 config=ap.ini + 代码设置的具体模块代码路径 ap.directory

     

        doucument_url是重写后的url request_url是重写前的url

        

        如果 config.type=string 则理解为pro.ini dev.ini 对于odp 则返回yaf_config_simaple_ce对象,并更新期_config属性为参数config数组 readonly=true

      

      1.2 request(yaf_request_instance->return yaf_request_http_instance(null, ap.base_url) odp中base_url=null 

          调用yaf_request_http_instance(null, base_url = null,request_url=null) 

          分析SG(request_info).method 记录

          uri = yaf_request_query_str(PATH_INFO) 分析PATH_INFO (内部通过PG php全局变量中的http_globals 获取$_SERVER变量值 http_globals存储了

SERVER GET POST值 然后设置base_url

    2 调用run方法      

         2.1 yaf_dispatcher_dispatch
        2.1.1 yaf_dispatcher_router 设置request中的 默认路由 以及module controller action 以及request的默认module controller action

                       读取request uri 和 base_uri 分析出module controller action 解析问号之前的 request_uri=uri  解析index.php 文件前面的作为

base-uri params似乎 总是为空

        2.1.2 yaf_dispatcher_handle

          

       1.3 controller/中未找到action方法 则到actions目录下寻找 (读取controllers中的 actions属性来确定寻找那个文件和对应 array

("actionName"=>"actions/file.php")

 

有些时候为了拆分比较大的Controller, 使得代码更加清晰和易于管理, Yaf支持将具体的动作分开定义. 每个动作都需要实现 Yaf_Action_Abstract 就可以通过定义

Yaf_Controller_Abstract::$actions来指明那些动作对应于具体的那些分离的类. 比如:

   

  插件需要注册(可不存放 app/newapp/plugins目录 下)

       yaf框架静态路由解析 如果request_url中 第一个 参数 对应module 不存在 则使用默认module controllers不合法(不在ap.modules配置中,未配置则为Null)

也使用默认的 后续多余的/...则作为params

       配置文件中的ap.modules 可以指定哪些是合法模块 a,b

      路由解析示例:

     location /newapp/ {                                                                                                                           

     

        root /home/users/pengzhi/odp/webroot;                                                                                                      

    

        index mp.php;                                                                                                                              

    

        fastcgi_pass $php_upstream;                                                                                                                

    

        #fastcgi_index   mp.php;# 已经排除请求fast_cig的url是 / 则添加index.php的情况                                                              

    

        include fastcgi.conf;                                                                                                                      

    

        rewrite ^/newapp(/[^\?]*)?((\?.*)?)$ /newapp/index.php$1$2 break; #跳出只处理 server块内容 即使其他location更加匹配重写后的uri             

                                                               

        #last 会重新处理这个server 不会出来后面的                                                                                                  

    

      }        

      http://cq01-rdqa-dev059.cq01.baidu.com:8125/newapp/Main/index3
     "SCRIPT_FILENAME": "/home/users/pengzhi/odp/webroot/newapp/index.php",

     "PATH_INFO": "/Main/index3",

     "SCRIPT_NAME": "/newapp/index.php",

     "REQUEST_URI": "/newapp/Main/index3",

     "DOCUMENT_URI": "/newapp/index.php/Main/index3",

     "DOCUMENT_ROOT": "/home/users/pengzhi/odp/webroot",

     "ORIG_PATH_INFO": "/newapp/index.php/Main/index", 

     "ORIG_SCRIPT_NAME": "/newapp/index.php/Main/index3",

     "ORIG_SCRIPT_FILENAME": "/home/users/pengzhi/odp/webroot/newapp/index.php/Main/index3",

     "PATH_TRANSLATED": "/home/users/pengzhi/odp/webroot/Main/index3",

     "PHP_SELF": "/newapp/index.php/Main/index3",

     

      def_module_name等属性是在框架初始化的时候设置

      base_url=""  yaf_dispatcher_route 设置 uri=/Main/index/ contstruct

      yaf_dispatcher_route(dispatcher, request) 调用yaf_static_router.yaf_router_pathinfo_router方法
除掉首尾/ =Main/Index  寻找 第一个/ 求子串Main判断是否是合法Module

     yaf_route_pathinfo_route 解析 module controller action  (如果解析到则更新 对应的 module controller action属性)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: