public function handle($params) { if ($this->_cName == null) { // above parseRoute() failed (controller not found) => run Default Controller (if specified) global $app_i; $def = trim($app_i['default_controller']); if ($def != '' && file_exists(BASE . $def)) { require_once BASE . $def; $ctr = new DefaultController(); $ctr->handle(null); $this->log->debug('Invalid URI. DefaultController called.'); return; } } // URI parsed OK! $fullpath = '/app/' . $this->_cPath . '/' . $this->_cName . '.php'; $this->log->debug('handle() route: ' . $fullpath); $fullpath = BASE . $fullpath; if (file_exists($fullpath)) { // load parsed Controller require_once $fullpath; $ctr = new $this->_cName(); $ctr->handle($this->_cParams); } else { die('MainController: Error: Controller not found!'); } $this->log->debug('handle() ended'); }
break; default: throw new Exception('API version must be specified', 404); break; } if (isset($parameters['oauth_version']) && $request->url_elements[2] != 'oauth') { $oauth_model = new OAuthModel(); $oauth_model->in_flight = true; $oauth_model->setUpOAuthAndDb($ji_db); $request->user_id = $oauth_model->user_id; } // Route: call the handle() method of the class with the first URL element if (isset($request->url_elements[2])) { $class = ucfirst($request->url_elements[2]) . 'Controller'; if (class_exists($class)) { $handler = new $class(); $return_data = $handler->handle($request, $ji_db); // the DB is set by the database config } else { throw new Exception('Unknown controller ' . $request->url_elements[2], 400); } } else { throw new Exception('Request not understood', 404); } } else { $defaultController = new DefaultController(); $return_data = $defaultController->handle($request, $ji_db); } // Handle output // TODO sort out headers, caching, etc $request->view->render($return_data);