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

一步步编写PHP的Framework(十二)

我上次在讲redirect和forward的时候我就说过,这两个函数要正常使用还需要修改一下route这个类,至少要将比如域名,控制器名,action名等存储起来,后面调用redirect,forward的时候可以使用。
        现在我们就转到route.php,原来这个类的代码很简单:
01 $action();
15             } else {
16                echo 'the method does not exists';
17             }
18          } else {
19             echo 'the class does not exists';
20          }
21       } else {
22          echo 'controller not exists';
23       }
24    }
25 }
现在我们需要将域名取出来,那怎么弄呢?
        实际上php有一个强大的超全局变量$_server,很多信息都存储在这里面,我们可以查看一下:
1 2 var_dump($_server);
我们注意到这里面有一个 http_host属性,查看php手册,这么写的:
        contents of the host: header from the current request, if there is one. 
    假设现在有一个url:http://localhost/test/test.php,那$_server['http_host']的值为什么呢,实际上为localhost。一般来说,我们想取到的是localhost/test,那么怎么获取后面的/test呢?
我们继续搜索一下:
     发现request_uri,script_filename,script_name,php_self的值都为/test/test.php,查询php手册解释分别为:
     1. the uri which was given in order to access this page; for instance, '/index.html'
     2. the absolute pathname of the currently executing script.
     3.contains the current script's path. this is useful for pages which need to point to themselves. the __file__ constant contains the full path and filename of the current (i.e. included) file.
     4. the filename of the currently executing script, relative to the document root. for instance, $_server['php_self'] in a script at the address http://example.com/test.php/foo.bar would be /test.php/foo.bar. the __file__ constant contains the full path and filename of the current (i.e. included) file. if php is running as a command-line processor this variable contains the script name since php 4.3.0. previously it was not available.
我们发现request_uri比较靠谱,当然,我这个地方测试的是apache的情况,nginx,iis等还有在.htaccess文件设置了rewrite规则后又不一样,如果真要写一个好的route,考虑的东西会非常多的,针对于url的普通模式,pathinfo模式,rewrite模式,兼容模式,我们使用最普通的方式。
       首先我们定义一个存储路径的类,path.php:
01 02 class path extends base {
03    private static $_base = '';
04    private static $_controller = '';
05    private static $_action = '';
06    public static function setbasepath($base) {
07       self::$_base = $base;
08    }
09    public static function setcontroller($controller) {
10       self::$_controller = $controller;
11    }
12    public static function setaction($action) {
13       self::$_action = $action;
14    }
15    public static function getbasepath() {
16       return self::$_base;
17    }
18    public static function getcontroller() {
19       return self::$_controller;
20    }
21    public static function getaction() {
22       return self::$_action;
23    }
24 }
就像java中pojo,这个类只有setter和getter,我就不多讲了。
        然后再看看route.php,首先还是获取url,怎么获取呢?
1 $_server['http_host'] . substr($_server['request_uri'],0,strrpos($_server['request_uri'],'/'))
由于之前已经讲了http_host和request_uri的作用了,这段代码主要就说一下后面的substr和strrpos,substr就是截断字符串,strrpos是获取某一个子字符串在父字符串中最后一次出现的位置。
        ps:我这样写得还是有问题的,但是为了简便,不弄复杂了。
       然后就是将这些值存储到path中,
1 path::setbasepath($_server['http_host'] . substr($_server['request_uri'],0,strrpos($_server['request_uri'],'/')));
2       path::setcontroller($controller);
3       path::setaction($action);
设置了这些参数之后,在controller.php中的redirect和forward的代码也要稍做修改:
01  $val) {
08          if(!is_int($key)) {
09             $str .= ($key . '=' . $val . '&');
10          }
11       }
12       $str = substr($str,0,strlen($str) - 1);
13       response::redirect($str);
14    }
15    protected function _forward(array $arr) {
16       $controller = path::getcontroller();
17       $action = path::getaction();
18       if(array_key_exists('controller',$arr)) {
19          $controller = $arr['controller'];
20       }
21       if(array_key_exists('action',$arr)) {
22          $action = $arr['action'];
23       }
24       $controller .= 'controller';
25       if($controller === get_class()) {
26          if(method_exists($this,$action)) {
27             $this->$action();
28          } else {
29             //时间有限,不写逻辑了
30          }
31       } else {
32          if(class_exists($controller)) {
33             $class = new $controller();
34             if(method_exists($class,$action)) {
35                $class->$action();
36             } else {
37                //时间有限,不写了
38             }
39          } else {
40             //时间有限,不写了
41          }
42       }
43    }
44    protected  function _assign(array $arr) {
45       view::assign($arr);
46    }
47    protected function _display($str) {
48       if(is_string($str)) {
49          $str = str_replace(array(
50             '.','#'
51          ),array(
52             '/','.'
53          ),$str);
54          view::display(modules_path . view::view_base_path . $str . '.php');
55       }
56    }
57 }
这个里面主要的改动就是控制器和action的获取变成了调用path类的方法,还有_redirect中,$str = 'http://' . path::getbasepath() . '/index.php?',这里我假设使用的时http协议,并且不存在rewrite,服务器采用的是apache。
       搞定之后再使用_redirect和_forward,发现是不是没有问题了?
其它类似信息

推荐信息