If the controller name is not found, it uses the default controller name
and if default controller name is not found in config file, it will use index as the
default controller.
Before proceeding to dispatcher, we must look at the view engine, which will be
used for template engine, so that anyone from controller can set variables like this
$this->view->set(varname, value). After that, anyone can access the variable as
$varname in our view file.
So here comes the view engine (core/main/view.php):
class view
{
private $vars=array();
private $template;
public function set($key, $value)
Chapter 9
[ 217 ]
{
$this->vars[$key]=$value;
}
public function getVars(&$controller=null)
{
if (!empty($controller)) $this->vars['app']=$controller;
return $this->vars;
}
public function setTemplate($template)
{
$this->template = $template;
}
public function getTemplate($controller=null)
{
if (empty($this->template)) return $controller;
return $this->template;
}
private function __get($var)
{
return loader::load($var);
}
}
?>
Here comes the dispatcher, the core part of our framework
(core/main/dispatcher.php):
class dispatcher
{
public static function dispatch($router)
{
global $app;
//$cache = loader::load("cache");
ob_start();
$config = loader::load("config");
if ($config->global_profile) $start = microtime(true);
$controller = $router->getController();
$action = $router->getAction();
$params = $router->getParams();
if (count($params)>1){
if ("unittest"==$params[count($params)-1] ||
'1'==$_POST['unittest'])unittest::setUp();
Building Better with MVC
[ 218 ]
}
$controllerfile = "app/controllers/{$controller}.
Pages:
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232