根据上话的目录结构(如果不清楚的可以看上一篇)。我在simple文件夹内新建了一个simple.php.
[php]
require 'includes/exceptions.php';
require 'includes/autoloader.php';
session_start();
//$view = new view();
lib::set_item('controller' , new controller());
lib::get_item('controller' , lib::persist_storge)->render();
//$content = $view->finish();
这一章主要是讲spl,所以我们只看第二行的autoloader.php.当我们在index.php中加入[php]
require 'simple/simple.php';
之后。
我们看一下autoloader.php的源码
[php]
class autoloader{
public static function includesautoloader($class){
$path = defined('simple_path') ? simple_path : $_server['document_root'];
$file_name = $path . '/includes/' . $class . '.php';
if(is_readable($file_name))
require $file_name;
}
public static function modulesautoloader($class){
$path = defined('simple_path') ? simple_path : $_server['document_root'];
$file_name = $path . '/modules/' . $class . '.php';
if(is_readable($file_name))
require $file_name;
}
public static function controllerautoloader($class){
$path = defined('simple_path') ? simple_path : $_server['document_root'];
$file_name = $path . '/controller/' . $class . '.php';
if(is_readable($file_name))
require $file_name;
}
}
spl_autoload_register('autoloader::includesautoloader');
spl_autoload_register('autoloader::modulesautoloader');
spl_autoload_register('autoloader::controllerautoloader');
这个类都是静态方法,当引用这个php文件后,程序会自动载入这三个文件夹下的内容,这样我们不用重复去required 或include。如果对spl有不了解的,可以直接去百度,这里只是告诉大家,这个spl作用相当广。
然后在includes文件夹内新建一个lib.php
[html]
analysis();
}
public function analysis(){
$path_info = $_server['path_info'];
if(substr($path_info , 0 , 1) == '/'){
$path_info = substr($path_info , 1);
}
$parts = explode('/' , $path_info);
if(emptyempty($parts[0])) $parts[0] = 'index';
if(emptyempty($parts[1])) $parts[1] = 'demo';
$this->parts = $parts;
array_shift($parts);
array_shift($parts);
$this->params = $parts;
}
public function render(){
if(!class_exists($this->parts[0])){
throw new controllerdoesntexistsexception($this->parts[0] . ' not exists!');
}
if(!method_exists($this->parts[0] , $this->parts[1])){
throw new actiondoesntexistsexception($this->parts[0] . 'of ' . $this->parts[1] . ' not exists!');
}
$new_controller = new $this->parts[0];
$called = call_user_func_array(array($new_controller , $this->parts[1]) , $this->params);
if($called === false){
throw new actionfailedexception($this->parts[0] . 'of ' . $this->parts[1] . ' failed to excute property!');
}
}
}
这个类只用path_info模式来获取调用的控制器和action,并将参数传递到相应的action方法里。当controller初始化之后,会自动分析path_info的内容,
lib::get_item('controller' , lib::persist_storge)->render();
然后调用render()就会在相应的控制器目录找到控制器和action。
例如我在controller文件夹内新建一个index.php
[php]
class index{
public function demo(){
echo sdfsdf;
}
}
那么执行http://localhost/index.php/index/demo就会输出sdfsdf;(这是我的本地路径)。
以上代码难度不高,如果没有看懂的同学,请q我,也可以去php官网上去查找相应函数的用法。
下一节相会对视图作一个简单的介绍。
作者:tomyjohn
http://www.bkjia.com/phpjc/478065.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/478065.htmltecharticle根据上话的目录结构(如果不清楚的可以看上一篇)。我在simple文件夹内新建了一个simple.php. [php] prequire includes/exceptions.php; require includes/...