推荐:《PHP视频教程》
首先看一下现有的文件目录

DOCUMENT_ROOR 为 /home/www目录
然后看一下入口文件的内容
<?php
$controll_action = $_GET['_ca_'];
$params = explode('/',$controll_action);
$params_count = count($params);
$otherParams = $params;
if($params_count>1) {
$controller = $params[0];
$action = $params[1];
unset($params[0]);
unset($params[1]);
}else if($params_count==1) {
$controller = $params[0];
$action = 'index';
unset($params[0]);
}
$filename = strtolower($controller).'.php';
$controller_path = $_SERVER['DOCUMENT_ROOT'].'/application/controllers/';
if(!file_exists($controller_path.$filename)) {
throw new Exception('controller '.$filename.' is not exists!');
return;
}
include($controller_path.$filename);
$classname = ucfirst($controller);
if(!class_exists($classname)) {
throw new Excpetion('class '.$classname.' is not exists!');
}
$reflecObj = new ReflectionClass($classname);
if(!$reflecObj->hasMethod($action)){
throw new Exception('method '.$action.' is not exists!');
}
$currentObj = new $classname();
echo "classname=$classname,action=$action,params=".json_encode($params)."<br/>";
call_user_func_array([$currentObj,$action],$params);
return;
?>然后创建一个简单的控制器 user.php,放到applicaiton/controllers/目录下,具体内容如下:
立即学习“PHP免费学习笔记(深入)”;
weenCompany闻名企业网站系统(免费开源)是一个功能强大, 使用简单的中英文企业智能建站系统, 您只需要一些基本的计算机知识就可以利用此系统完成中小型企业网站的建设; 是低成本企业网站架设方案之首选CMS系统, 也适合建设个人网站。weenCompany闻名企业网站系统功能:1. 程序代码简洁严谨, 整个系统程序仅2M左右大小.2. 中英文双语版共用一套网站程序, 双语页面实现自由切换.3
0
<?php
class User {
function __construct(){
}
public function index($name='')
{
echo 'hello,'.$name.',lucky,you are arrive here!';
}
}最后测试一个正确的控制器跳转和错误的控制器跳转
首先测试一下正确的流程: http://192.168.1.99/user/index/xiaoming
输出内容:
classname=User,action=index,params={"2":"xiaoming"}
hello,xiaoming,lucky,you are arrive here!再测试一下不存在的控制器,http://192.168.1.99/account/index/xiaoming
Fatal error: Uncaught exception 'Exception' with message 'controller acount.php is not exists!' in /home/www/webroot/index.php:25Stack trace:#0 {main} thrown in
/home/www/webroot/index.php on line 25以上就是一个php框架的简单实现,仅实现简单路由层的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号