仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1400|回复: 19
打印 上一主题 下一主题

[学习教程] PHP网页编程之一步步编写PHP的Framework(十二)

[复制链接]
海妖 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-16 00:22:10 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
HTML中的任何元素都要亲自实践,只有明白了什么元素会起到什么效果之后,你才会记忆深刻,而一味的啃书,绝对是不行的,我想大部分新手之所以觉得概念难学,大部分是一个字“懒”,懒是阻止进步的最大敌人,所以克服掉懒的习惯,才能更快的学好一样东西。     
          我前次在讲redirect和forward的时分我就说过,这两个函数要正常利用还需求修正一下Route这个类,最少要将好比域名,掌握器名,Action名等存储起来,前面挪用redirect,forward的时分可使用。
          如今咱们就转到Route.php,本来这个类的代码很复杂:
01 <?php 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 <?php 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 <?php 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 <?php 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,发明是否是没有成绩了?
也许您在学习PHP的时候只想尽快的开发一个网站,也就会想我做网站,干嘛要学什么网页这些小儿科?不难看出,眼高手低的新手不在少数,这种思想无疑于建造空中楼阁,你不建地基,何来的房顶呢?
海妖 该用户已被删除
沙发
 楼主| 发表于 2015-2-16 00:23:57 | 显示全部楼层
这些中手常用的知识,当你把我说的这些关键字都可以熟练运用的时候,你可以选择自己
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-6-7 09:22

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表