哪里錯(cuò)了啊?
這是我的結(jié)構(gòu)文件:/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); ?>
報(bào)的錯(cuò)誤信息:
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
哪里錯(cuò)了啊……看不出來(lái)……
解決方案:
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; ????} ?>
2015-10-01
路徑問(wèn)題。在linux里面 require 使用絕對(duì)路徑 / 開(kāi)頭的話,會(huì)從linux的根目錄開(kāi)始找,而不是你網(wǎng)站的根目錄。