我的 MVC 结构如下:
- web
-- Classes
--- Loader.php
-- Core
--- Controller.php
--- Model.php
--- View.php
--- Bootstrap.php
--- DB.php
-- Project
--- Controllers (folder)
--- Models (folder)
--- Views (folder)
现在我为每个指定了命名空间。例如我有
namespace Classes; //for Loader.php
namespace Core; //For Controller.php, Model.php etc...
namespace Project\Controllers; //For Controllers inside Controllers folder etc...
我的自动加载器看起来像这样:
public static function Autoloader2($className) {
$className = explode('\\', $className);
$class = array_pop($className);
$namespace = implode(DIRECTORY_SEPARATOR, $className);
$file = $namespace . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
require $file;
}
和我的主要 index.php 使用
spl_autoload_register( array('Loader', 'Autoloader2') );
$app = new Core\Bootstrap();
当我打开 localhost/web/ 时,出现以下错误:
Warning: require(Core/Project/Controllers/Index.php): failed to open stream: N
它似乎将 Core/ 添加到请求的文件中。Index.php 在 Project/Controllers 中,而不是在 Core/Project/Controllers 中。如果我尝试删除
//namespace Core; from Bootstrap.php i get the following error
Fatal error: Class 'Core\Bootstrap' not found
请帮忙