最简PHP框架
好久没更新了,一是自已越来越懒,二是,这段时间总是心烦意乱,很难静下心来。
不说了,看下面这个简单的php框架。虽然只有短短的137个字符(完全可以做为一条tweet),但却可以大致模似出CodeIgniter的用法。
1 2 3 4 5 6 | <?php $g=$GET;$c=@$g['c']?:'home'; if(!@require "c/$c.php") die('error'); $m=method_exists($c,@$g['m'])?$g['m']:'index'; $o=new $c; $o->$m($g); ?> |
将以上代码保存为index.php,然后在index.php所在目录下新建C文件夹,把以下代码保存在C目录中,文件名为home.php。
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php class home { function index() { echo "hello, world! fucking gfw!" } function user($para) { echo 'hello, '.htmlspecialchars($para['name']); } } ?> |
这样,当访问index.php时,将会执行默认控制器home的默认方法index。
访问index.php?m=user&name=sallon时将会执行home->user($para)方法。
新建控制器时,只需在index.php后指定c=’控制器名’就可以了。
