哪里错了啊?
这是我的结构文件:/var/www/html/test/MVC/
├── test
│ ├── MVC
│ │ ├── Controller
│ │ │ └── testController.class.php
│ │ ├── function.php
│ │ ├── index.php
│ │ ├── Model
│ │ │ └── testModel.class.php
│ │ ├── test.php
│ │ └── View
│ │ └── testView.class.php
│ └── others
/test/MVC/Controller/testController.class.php
<?php class testController{ function show(){ //$testModel = new testModel(); $testModel = Model('test'); $data=$testModel->get(); //$testView = new testView(); $testView = View('test'); $testView->display($data); } } ?>
/test/MVC/Model/testModel.class.php
<?php class testModel{ function get(){ echo "Hello World!"; } } ?>
/test/MVC/View/testView.class.php
<?php class testView{ function display($data){ echo $data; } } ?>
function.php
<?php function Controller($name,$method){ require_once('/test/MVC/Controller/'.$name.'Controller.class.php'); //$testController = new testController(); //$testController->show(); eval('$obj = new'.$name.'Controller(); $obj->'.$method.'();'); } Controller('test','show'); function Model($name){ require_once('/test/MVC/Model/'.$name.'Model.class.php'); //$testModel = new testModel(); eval('$obj = new '.$name.'Model();'); return $obj; } function View($name){ require_once('/test/MVC/View/'.$name.'View.class.php'); //$testView = new testView(); eval('$obj = new '.$name.'View();'); return $obj; } function daddslashes($str){ return (!get_magic_quotes_gpc())?addslashes($str):$str; } ?>
index.php
<?php //url形式 index.php?controller=控制器名&method=方法名 require_once('function.php'); $controllerAllow = array('test','index'); $methodAllow = array('test','index','show'); $controller = in_array($_GET['controller'], $controllerAllow)?daddslashes($_GET['controller']):'index' ; $method = in_array($_GET['method'],$methodAllow)?daddslashes($_GET['method']):'index'; Controller($controller,$method); ?>
报的错误信息:
http://127.0.0.1/test/MVC/
Warning: require_once(/test/MVC/Controller/testController.class.php): failed to open stream: No such file or directory in /var/www/html/test/MVC/function.php on line 3 Fatal error: require_once(): Failed opening required '/test/MVC/Controller/testController.class.php' (include_path='.:/usr/local/php/lib/php') in /var/www/html/test/MVC/function.php on line 3
哪里错了啊……看不出来……
解决方案:
function.php
<?php function Controller($name,$method){ require_once('Controller/'.$name.'Controller.class.php'); //$testController = new testController(); //$testController->show(); eval('$obj = new'.$name.'Controller(); $obj->'.$method.'();'); } Controller('test','show'); function Model($name){ require_once('Model/'.$name.'Model.class.php'); //$testModel = new testModel(); eval('$obj = new '.$name.'Model();'); return $obj; } function View($name){ require_once('View/'.$name.'View.class.php'); //$testView = new testView(); eval('$obj = new '.$name.'View();'); return $obj; } function daddslashes($str){ return (!get_magic_quotes_gpc())?addslashes($str):$str; } ?>