怎么一步步编写简单的PHP的Framework(十二)

      昨天好好耍了一天,今天早上准备到公司学习一些东西,看着时间还早,就果断写一篇。 php

       我上次在讲redirect和forward的时候我就说过,这两个函数要正常使用还须要修改一下Route这个类,至少要将好比域名,控制器名,Action名等存储起来,后面调用redirect,forward的时候可使用。 html

        如今咱们就转到Route.php,原来这个类的代码很简单: nginx


<?php
class Route extends Base {
   public static function run() {
      $controller = empty($_GET['c']) ? C('defaultController') : trim($_GET['c']); //设置了默认的控制器
      $action = empty($_GET['a']) ? C('defaultAction') : trim($_GET['a']); //设置了默认的Action
      $controllerBasePath = APP_PATH . '/UserApps/Modules/Controllers/';
      $controllerFilePath = $controllerBasePath . $controller . 'Controller.php';
      if(is_file($controllerFilePath)) {
         include $controllerFilePath;
         $controllerName = $controller . 'Controller';
         if(class_exists($controllerName)) {
            $controllerHandler = new $controllerName();
            if(method_exists($controllerHandler,$action)) {
               $controllerHandler->$action();
            } else {
               echo 'the method does not exists';
            }
         } else {
            echo 'the class does not exists';
         }
      } else {
         echo 'controller not exists';
      }
   }
}


        如今咱们须要将域名取出来,那怎么弄呢? apache

        实际上PHP有一个强大的超全局变量$_SERVER,不少信息都存储在这里面,咱们能够查看一下: 服务器


<?php
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. this

     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. spa

     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:


<?php
class Path extends Base {
   private static $_base = '';
   private static $_controller = '';
   private static $_action = '';
   public static function setBasePath($base) {
      self::$_base = $base;
   }
   public static function setController($controller) {
      self::$_controller = $controller;
   }
   public static function setAction($action) {
      self::$_action = $action;
   }
   public static function getBasePath() {
      return self::$_base;
   }
   public static function getController() {
      return self::$_controller;
   }
   public static function getAction() {
      return self::$_action;
   }
}
        就像Java中pojo,这个类只有setter和getter,我就很少讲了。


        而后再看看Route.php,首先仍是获取URL,怎么获取呢?

  

$_SERVER['HTTP_HOST'] . substr($_SERVER['REQUEST_URI'],0,strrpos($_SERVER['REQUEST_URI'],'/'))


        因为以前已经讲了HTTP_HOST和REQUEST_URI的做用了,这段代码主要就说一下后面的substr和strrpos,substr就是截断字符串,strrpos是获取某一个子字符串在父字符串中最后一次出现的位置。

        PS:我这样写得仍是有问题的,可是为了简便,不弄复杂了。

       而后就是将这些值存储到Path中,


Path::setBasePath($_SERVER['HTTP_HOST'] . substr($_SERVER['REQUEST_URI'],0,strrpos($_SERVER['REQUEST_URI'],'/')));
      Path::setController($controller);
      Path::setAction($action);
       设置了这些参数以后,在Controller.php中的redirect和forward的代码也要稍作修改:



<?php
class Controller extends Base {
   protected function _redirect(Array $arr) {
      array_key_exists('controller',$arr) || $arr['controller'] = Path::getContrller();
      array_key_exists('action',$arr) || $arr['action'] = Path::getAction();;
      $str = 'http://' . Path::getBasePath() . '/index.php?';
      foreach($arr as $key => $val) {
         if(!is_int($key)) {
            $str .= ($key . '=' . $val . '&');
         }
      }
      $str = substr($str,0,strlen($str) - 1);
      Response::redirect($str);
   }
   protected function _forward(Array $arr) {
      $controller = Path::getController();
      $action = Path::getAction();
      if(array_key_exists('controller',$arr)) {
         $controller = $arr['controller'];
      }
      if(array_key_exists('action',$arr)) {
         $action = $arr['action'];
      }
      $controller .= 'Controller';
      if($controller === get_class()) {
         if(method_exists($this,$action)) {
            $this->$action();
         } else {
            //时间有限,不写逻辑了
         }
      } else {
         if(class_exists($controller)) {
            $class = new $controller();
            if(method_exists($class,$action)) {
               $class->$action();
            } else {
               //时间有限,不写了
            }
         } else {
            //时间有限,不写了
         }
      }
   }
   protected  function _assign(Array $arr) {
      View::assign($arr);
   }
   protected function _display($str) {
      if(is_string($str)) {
         $str = str_replace(array(
            '.','#'
         ),array(
            '/','.'
         ),$str);
         View::display(MODULES_PATH . View::VIEW_BASE_PATH . $str . '.php');
      }
   }
}
       这个里面主要的改动就是控制器和Action的获取变成了调用Path类的方法,还有_redirect中,$str = 'http://' . Path::getBasePath() . '/index.php?',这里我假设使用的时http协议,而且不存在rewrite,服务器采用的是apache。


       搞定以后再使用_redirect和_forward,发现是否是没有问题了?

       代码点此下载

相关文章
相关标签/搜索