0

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);
        }
    }
}
?>
AdRock
  • 2,959
  • 10
  • 66
  • 106
  • I bet that you are now wishing you had put all your require()'s at the top of each file :) – vascowhite Sep 22 '13 at 09:32
  • That's why i want to use an autoloader so I don't need to – AdRock Sep 22 '13 at 09:36
  • Just remove all the `require`'s (which should have been `require_once`'s in the first place). However, this code never worked properly: `if(!file_exists($somefile)) require $somefile;`. – Wrikken Sep 22 '13 at 09:40

0 Answers0