昨天好好耍了一天,今天早上准备到公司学习一些东西,看着时间还早,就果断写一篇。
我上次在讲redirect和forward的时候我就说过,这两个函数要正常使用还需要修改一下Route这个类,至少要将比如域名,控制器名,Action名等存储起来,后面调用redirect,forward的时候可以使用。
现在我们就转到Route.php,原来这个类的代码很简单:
<?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';
}
}
}
现在我们需要将域名取出来,那怎么弄呢?
实际上PHP有一个强大的超全局变量$_SERVER,很多信息都存储在这里面,我们可以查看一下:
<?php
var_dump($_SERVER);
我们注意到这里面有一个
HTTP_HOST属性,查看PHP手册,这么写的:
我们继续搜索一下:
发现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:
<?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,发现是不是没有问题了?
代码点此下载