怎麼一步步編寫簡單的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,發現是否是沒有問題了?

       代碼點此下載

相關文章
相關標籤/搜索