I have an autoloader.php in the root of my project:
<?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';
}
in my webroot folder I have an index.php file where I require this autoloader:
require_once '../autoload.php';
now in the root of my project I have a controllers folder and an adminContoller.php file that looks like this:
<?php
namespace controllers;
class adminController extends Controller
{
now I get these errors:
Warning: require_once(adminController.php): failed to open stream: No such file or directory in /some folders/autoload.php on line 9
Fatal error: require_once(): Failed opening required 'adminController.php' (include_path='.:/usr/share/pear:/usr/share/php') in /some folders/autoload.php on line 9
this is line 9 in autoload.php require_once $file . '.php';
my structure:
EDIT 1 full index.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();
EDIT 2 full dispatcher.php file:
<?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;
}
}