为了账号安全,请及时绑定邮箱和手机立即绑定

哪里错了啊?

这是我的结构文件:/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;
    }

?>


正在回答

7 回答

路径问题。在linux里面 require 使用绝对路径 / 开头的话,会从linux的根目录开始找,而不是你网站的根目录。

0 回复 有任何疑惑可以回复我~

谢谢你的支持。由于这个课程在设计之初主要是启发大家从头制作一个游戏,所以很多复杂的技术没有使用,在代码层面有非常多的优化空间。

blsji

0 回复 有任何疑惑可以回复我~

require_once('Controller/'.$name.'Controller.class.php');
换成这个就打印出Hello World!了!

0 回复 有任何疑惑可以回复我~

  require_once('/test/MVC/Controller/'.$name.'Controller.class.php');改成

  require_once('/Controller/'.$name.'Controller.class.php');试试

0 回复 有任何疑惑可以回复我~
#1

全民工作狂 提问者

然而并没有用……
2015-10-08 回复 有任何疑惑可以回复我~
#2

全民工作狂 提问者

require_once('Controller/'.$name.'Controller.class.php'); 换成这个就打印出Hello World!了!
2015-10-08 回复 有任何疑惑可以回复我~
#3

晚安sp 回复 全民工作狂 提问者

嗯,你就是路径弄错了
2015-10-09 回复 有任何疑惑可以回复我~

      require_once('/test/MVC/Controller/'.$name.'Controller.class.php');这个的话,确实写得没问题吗?

0 回复 有任何疑惑可以回复我~
#1

全民工作狂 提问者

-。-真的觉得这句没有问题啊……你用你的火眼金睛看看……我已经晕了……
2015-09-30 回复 有任何疑惑可以回复我~
#2

乔帮主

回复 全民工作狂你看下它的两个提示,都是说的你的这个 require_once('/test/MVC/Controller/'.$name.'Controller.class.php');出问题了!你再弄下看
2015-09-30 回复 有任何疑惑可以回复我~

你还是检查一下目录权限吧。

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

哪里错了啊?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信