欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

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

程序员文章站 2024-04-04 15:44:35
...
我上次在讲redirect和forward的时候我就说过,这两个函数要正常使用还需要修改一下Route这个类,至少要将比如域名,控制器名,Action名等存储起来,后面调用redirect,forward的时候可以使用。

现在我们就转到Route.php,原来这个类的代码很简单:

01

02 class Route extends Base {

03 public static function run() {

04 $controller= empty($_GET['c']) ? C('defaultController') : trim($_GET['c']); //设置了默认的控制器

05 $action = empty($_GET['a']) ? C('defaultAction') : trim($_GET['a']); //设置了默认的Action

06 $controllerBasePath = APP_PATH . '/UserApps/Modules/Controllers/';

07 $controllerFilePath = $controllerBasePath . $controller . 'Controller.php';

08 if(is_file($controllerFilePath)) {

09 include $controllerFilePath;

10 $controllerName = $controller . 'Controller';

11 if(class_exists($controllerName)) {

12 $controllerHandler = new $controllerName();

13 if(method_exists($controllerHandler,$action)) {

14 $controllerHandler->$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

02 class Controller extends Base {

03 protected function _redirect(Array $arr) {

04 array_key_exists('controller',$arr) $arr['controller'] = Path::getContrller();

05 array_key_exists('action',$arr) $arr['action'] = Path::getAction();;

06 $str = 'http://' . Path::getBasePath() . '/index.php?';

07 foreach($arr as $key => $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,发现是不是没有问题了?