I am trying to add an autoloader to the loader of my MVC to remove all the need for requires and includes.
The autoloader works because I've used it before.
What I need to know is how I can update this code to remove all the 'require's as they should not be needed.
The bit that needs to be changed, I think, is just inside the createController() method where it checks if the controllers class file exists
<?php
class Loader {
private $controllerName;
private $controllerClass;
private $action;
private $urlValues;
//store the URL request values on object creation
public function __construct() {
$this->urlValues = $_GET;
if ($this->urlValues['controller'] == "") {
$this->controllerName = "home";
$this->controllerClass = "HomeController";
} else {
$this->controllerName = strtolower($this->urlValues['controller']);
$this->controllerClass = ucfirst(strtolower($this->urlValues['controller'])) . "Controller";
}
if ($this->urlValues['action'] == "") {
$this->action = "index";
} else {
$this->action = $this->urlValues['action'];
}
}
//factory method which establishes the requested controller as an object
public function createController() {
//check our requested controller's class file exists and require it if so
if (!file_exists("controllers/class." . $this->controllerName . ".php")) {
require("controllers/class." . $this->controllerName . ".php");
} else {
require("controllers/class.error.php");
return new ErrorController("badurl",$this->urlValues);
}
//does the class exist?
if (class_exists($this->controllerClass)) {
$parents = class_parents($this->controllerClass);
//does the class inherit from the BaseController class?
if (in_array("BaseController",$parents)) {
//does the requested class contain the requested action as a method?
if (method_exists($this->controllerClass,$this->action))
{
return new $this->controllerClass($this->action,$this->urlValues);
} else {
//bad action/method error
//require("controllers/error.php");
return new ErrorController("badurl",$this->urlValues);
}
} else {
//bad controller error
//require("controllers/error.php");
return new ErrorController("badurl",$this->urlValues);
}
} else {
//bad controller error
//require("controllers/error.php");
return new ErrorController("badurl",$this->urlValues);
}
}
}
?>