I am creating a basic MVC structured CMS in PHP as a means to learn how MVC works (thus the reason I am not using a true prebuilt engine). I have a basic version working that is in structure very similar to this tutorial here. I'd like however for the views to be loaded automatically, bypassing the need for a template class. If this is strongly recommended I'll stick with the template concept (if someone could explain WHY it's so necessary I would greatly appreciate it.) Anyways, below is my router class which I have modified to automatically load the view files along the controller.
public function loader() {
/*** check the route ***/
$this->getPath();
/*** if the file is not there diaf ***/
if (is_readable($this->controller_path) == false) {
$this->controller_path = $this->path.'/controller/error404.php';
$this->action_path = $this->path.'/view/error404.php';
}
/*** include the path files ***/
include $this->controller_path;
include $this->action_path;
/*** a new controller class instance ***/
$class = $this->controller . 'Controller';
$controller = new $class($this->registry);
/*** check if the action is callable ***/
if (is_callable(array($controller, $this->action)) == false) {
$action = 'index';
} else {
$action = $this->action;
}
$controller->$action();
}
/**
*
* @get the controller
*
* @access private
*
* @return void
*
*/
private function getPath() {
/*** get the route from the url ***/
$route = (empty($_GET['rt'])) ? '' : $_GET['rt'];
if (empty($route)) {
$route = 'index';
} else {
/*** get the parts of the route ***/
// mywebsite.com/controller/action
// mywebsite.com/blog/hello
$parts = explode('/', $route);
$this->controller = $parts[0];
if(isset( $parts[1])) {
$this->action = $parts[1];
}
}
if( ! $this->controller ) { $this->controller = 'index'; }
if( ! $this->action ) { $this->action = 'index'; }
/*** set the file path ***/
$this->controller_path = $this->path .'/controller/'. $this->controller . '.php';
$this->action_path = $this->path .'/view/'. $this->controller . '/'. $this->action . '.php';
}
This is preventing my view files from loading the variables given by the controller (the tutorial website has a better demonstration of this) but when setting $this->registry->template->blog_heading = 'This is the blog Index';
the view doesn't load it because the template.class is bypassed. Basically what I'm asking is how do shift the template.class over into the loading functions?