Jump to content

Controller Namespace Gets Appended to Model Namespace


NomadicJosh

Recommended Posts

I've managed to get my framework to adhere to PSR-0, however, I have an issue that I can't seem to resolve at the moment. The issue is between my bootstrap.php and my controller.php files. The bootstrap includes a namespace to controller and the controller includes a namespace to the model(s) but the controller namespace is being appended to the model(s) namespace (due to loadModel($name)) causing it to look for the model in the controller directory. Below are the files. Can any see how I can maybe fix this issue. Thanks.

 

Bootstrap.php:

class Bootstrap {

    public function __construct() {

        $url = isset($_GET['url']) ? $_GET['url'] : null;
        $url = rtrim($url, '/');
        $url = filter_var($url, FILTER_SANITIZE_URL);
        $url = explode('/', $url);

        if (empty($url[0])) {
            $controller = new \tinyPHP\Classes\Controllers\Index();
            $controller->index();
            return false;
        }

        $file = SYS_PATH . 'Classes' . DS . 'Controllers' . DS . $url[0] . '.php';
        if (file_exists($file)) {
            require $file;
        } else {
            $this->error();
        }

        $url[0] = "\\tinyPHP\\Classes\\Controllers\\".$url[0];
        $controller = new $url[0];
        $controller->loadModel($url[0]);

        // calling methods
        if (isset($url[2])) {
            if (method_exists($controller, $url[1])) {
                $controller->{$url[1]}($url[2]);
            } else {
                $this->error();
            }
        } else {
            if (isset($url[1])) {
                if (method_exists($controller, $url[1])) {
                    $controller->{$url[1]}();
                } else {
                    $this->error();
                }
            } else {
                $controller->index();
            }
        }


    }

    public function error() {
        $controller = new \tinyPHP\Classes\Controllers\Error();
        $controller->index();
        return false;
    }

}

 

Controller.php:

class Controller {

    public function __construct() {
        $this->view = new \tinyPHP\Classes\Core\View();
    }

    public function loadModel($name) {

            $modelName = $name . 'Model';
            $modelName = "\\tinyPHP\\Classes\\Models\\".$modelName;
            $this->model = new $modelName();    
    }

}

Just in case its needed, this is the error message I get when I visit http://localhost:8888/tphp/user/

 

Unable to load userModel
Fatal error: Class '\tinyPHP\Classes\Models\\tinyPHP\Classes\Controllers\userModel' not found in /Applications/MAMP/htdocs/tPHP/tinyPHP/Classes/Core/Controller.php on line 37

$url[0] = "\\tinyPHP\\Classes\\Controllers\\".$url[0];
$controller = new $url[0];
$controller->loadModel($url[0]);

 

You see WHY this is happening, right?

 

$loadController = "\\tinyPHP\\Classes\\Controllers\\".$url[0];
$name = $url[0];
$controller = new $loadController;
$controller->loadModel($name);

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.