我的类自动加载功能在我的mvc中不起作用


My class autoload function is not working in my mvc

MVC在cloud9上上传,但位于lib/init.php中的自动加载类不工作时。项目文件夹或webroot文件夹文件中的.htaccess配置不正确。

当我访问我的项目链接时https://my-mvc-hunteelar.c9users.io/它给出了这个例外:

无法在第21行的/home/ubuntu/workspace/lib/init.php中加载类:配置

我有两个.htaccess文件。主项目文件夹中的第一个.htaccess代码是:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^$ webroot/ [L]
    RewriteRule (.*) webroot/$1 [L]
</IfModule>

位于webroot文件夹中的第二个.htaccess文件,该文件夹应该是我的MVC的公共文件夹:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php [PT,L] 
</IfModule>

包含__autoload函数的init.php代码。

require_once (ROOT.DS.'config'.DS.'config.php');
function __autoload($class_name){
    $lib_path = ROOT.DS.'lib'.DS.strtolower($class_name).'class.php';
    $controllers_path = ROOT.DS.'controllers'.DS.str_replace('controller', '', strtolower($class_name)).'class.php';
    $models_path = ROOT.DS.'models'.DS.strtolower($class_name).'class.php';
    if(file_exists($lib_path)){
        require ($lib_path);
    }
    elseif (file_exists($controllers_path)){
        require_once ($controllers_path);
    }
    elseif (file_exists($models_path)){
        require_once ($models_path);
    }
    else{
            throw new Exception('Failed to load class : '.$class_name);
        }
}

这是我在cloud9上传的代码https://ide.c9.io/hunteelar/my-mvc

我想你已经解决了这个问题。但问题在于代码的初始化。

$controllers_path = ROOT.DS.'controllers'.DS.str_replace('controller', '', strtolower($class_name)).'class.php';

实际上应该是:

$controllers_path = ROOT.DS.'controllers'.DS.str_replace('controller', '', strtolower($class_name)).'.controller.php';

还有

$models_path = ROOT.DS.'models'.DS.strtolower($class_name).'class.php';

应该是

$model_path = ROOT.DS.'models'.DS.strtolower($class_name).'.php';

万一它能帮助别人。