NomadicJosh Posted July 31, 2012 Share Posted July 31, 2012 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 https://forums.phpfreaks.com/topic/266499-controller-namespace-gets-appended-to-model-namespace/ Share on other sites More sharing options...
NomadicJosh Posted July 31, 2012 Author Share Posted July 31, 2012 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 Link to comment https://forums.phpfreaks.com/topic/266499-controller-namespace-gets-appended-to-model-namespace/#findComment-1365709 Share on other sites More sharing options...
Jessica Posted July 31, 2012 Share Posted July 31, 2012 $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 https://forums.phpfreaks.com/topic/266499-controller-namespace-gets-appended-to-model-namespace/#findComment-1365760 Share on other sites More sharing options...
NomadicJosh Posted July 31, 2012 Author Share Posted July 31, 2012 Bless you @jesirose. This is why I needed an extra pair of eyes. Link to comment https://forums.phpfreaks.com/topic/266499-controller-namespace-gets-appended-to-model-namespace/#findComment-1365766 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.