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();    
    }

}

Link to comment
Share on other sites

$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);

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.