我在项目的根目录中有一个autoloader.php:
<?php
spl_autoload_register('AutoLoader');
function AutoLoader($className)
{
// imports files based on the namespace as folder and class as filename.
$file = str_replace('\\',DIRECTORY_SEPARATOR, $className);
require_once $file . '.php';
}
在我的webroot文件夹中,我有一个index.php文件,需要该自动加载器:
require_once '../autoload.php';
现在在我项目的根目录中,我有一个controllers文件夹和一个adminContoller.php文件,如下所示:
<?php
namespace controllers;
class adminController extends Controller
{
现在我得到这些错误:
警告:require_once(adminController.php):无法打开流:在第9行的/ some folder / autoload.php中没有此类文件或目录
严重错误:require_once():无法在第9行的/ some folder / autoload.php中打开所需的'adminController.php'(include_path ='。:/ usr / share / pear:/ usr / share / php')
这是autoload.php require_once $file . '.php';
我的结构:
<?php
require_once '../autoload.php';
define('WEBROOT', str_replace("webroot/index.php", "", $_SERVER["SCRIPT_NAME"]));
define('ROOT', str_replace("webroot/index.php", "", $_SERVER["SCRIPT_FILENAME"]));
//require(ROOT . 'config/core.php');
require(ROOT . 'router.php');
require(ROOT . 'request.php');
require(ROOT . 'dispatcher.php');
$dispatch = new Dispatcher();
$dispatch->dispatch();
<?php
class Dispatcher
{
private $request;
public function dispatch()
{
$this->request = new Request();
Router::parse($this->request->url, $this->request);
$controller = $this->loadController();
call_user_func_array([$controller, $this->request->action], $this->request->params);
}
public function loadController()
{
$name = $this->request->controller . "Controller";
$file = ROOT . 'Controllers/' . $name . '.php';
require($file);
$controller = new $name();
return $controller;
}
}