/** * Routing the controller actions based on url, to run a class method in question. * * @return void * @throws Exception * @throws NotFoundException * @throws MemberInaccessibleException */ public function dispatch() { Loader::load('Request', 'core'); Loader::load('ViewAbstract', 'core'); $request = new Request(); $response = new Response(); $view = null; $viewInitialized = false; $subdir = ''; try { // check system root setting. $sysRoot = PathManager::getSystemRoot(); if ($sysRoot == null) { throw new Exception('Path of the system root has not been specified.'); } else { if (!file_exists($sysRoot)) { throw new Exception('Path of the system root does not exist.'); } } $router = new Router(); // initialize by Initializer. $initializer = $this->_searchIntializer(); if ($initializer instanceof InitializerStandard) { $initializer->setDispatcher($this); $initializer->setRouter($router); $initializer->applyConfig(); $initializer->initEnv(); $initializer->initialize(); } $params = $router->route($request->getBasePath()); // create view class instance. Loader::load($this->_viewClassName, null, true, false); $viewClass = $this->_viewClassName; $view = new $viewClass(); // set request instance to controller. $subdir = $params['subdir']; $request->setController($params['controller'], $subdir); $request->setAction($params['action']); $request->setParams($params['params']); $view->setRequest($request); $view->initialize(); $viewInitialized = true; // create controller class instance. $controller = $this->_getControllerInstance($params['controller'], $params['subdir']); if ($controller == false) { throw new NotFoundException($params['controller'], ''); } $controller->setAppEnv($this->_appEnv); $controller->setRequest($request); // set response instance to controller. $controller->setResponse($response); // set view to controller. $controller->setView($view); // get action method name. $reqestMethod = null; $actionMethod = ''; if ($controller instanceof RestController) { $request->isRestAccess(true); $reqestMethod = $request->getMethod(); if ($params['action'] == 'index' && $reqestMethod != 'GET') { $restDefaultMethod = NameManager::convertActionToMethod($reqestMethod); if (method_exists($controller, $restDefaultMethod)) { $actionMethod = $restDefaultMethod; } } } if ($actionMethod == '') { $actionMethod = NameManager::convertActionToMethod($params['action'], $reqestMethod); } // initialize validator $validatorClass = $this->_validatorClassName; $validator = new $validatorClass(); $controller->setValidator($validator); $controller->initValidator(); $controller->initialize(); // Create plugin instance. $plugin = null; if ($this->_pluginEnabled) { $plugin = $this->_searchPlugin($params['subdir']); if ($plugin instanceof PluginAbstract) { $plugin->setControllerInstance($controller); $plugin->preProcess(); } } $controller->preProcess(); if ($controller->isDispatched() == false) { // Check action method exists. if (!method_exists($controller, $actionMethod)) { $controllerParam = $params['controller']; if ($params['subdir'] != null) { $controllerParam = $params['subdir'] . '/' . $controllerParam; } throw new NotFoundException($controllerParam, $params['action']); } else { if (!is_callable(array($controller, $actionMethod))) { throw new MemberInaccessibleException($params['controller'], $params['action']); } } $controller->{$actionMethod}(); } $controller->postProcess(); if ($this->_pluginEnabled) { if ($plugin instanceof PluginAbstract) { $plugin->postProcess(); } } if ($controller instanceof RestController) { if ($controller->isAutoRedirect() == true && $request->getMethod() != 'GET') { $url = ''; if ($params['action'] != 'index') { $url = $params['controller'] . '/' . $params['action']; } else { if ($params['controller'] != 'index') { $url = $params['controller']; } } $url = $request->getBaseUrl() . '/' . $url; $response->redirect($url); } } $response->send(); if ($response->isOutput() == false && $view->getRenderingEnabled() == true) { $viewScript = $this->_getViewScriptInstance($params['controller'], $params['subdir']); if ($viewScript instanceof ViewScript) { if (method_exists($viewScript, $actionMethod)) { $viewScript->setView($view); $viewScript->setRequest($request); $viewScript->{$actionMethod}(); } } $view->render(); } } catch (Exception $e) { $handler = $this->_searchErrorHandler($subdir); if ($handler instanceof ErrorHandlerStandard) { $action = 'error'; $actionMethod = ''; $className = get_class($e); if ($className != 'Exception') { $actionMethod = str_replace('Exception', '', $className); $actionMethod = NameManager::toMethod($actionMethod); } if (method_exists($handler, $actionMethod)) { $action = $actionMethod; } $handler->setException($e); $handler->setRequest($request); $handler->setResponse($response); if ($viewInitialized == true) { $handler->setView($view); } if ($action == 'notFound') { if ($this->_isSend404 == true) { $response->setHttpStatus('404'); $response->send(); } } $handler->{$action}(); } else { throw $e; } } }
<?php require_once '/nak/webapp/bootstrap.php'; if (!session_id()) { session_start(); } $cur_stage = NetAidManager::get_stage(); $request = new Request($_GET['query']); $dispatcher = new Dispatcher(); $controller = $request->getController(); $action = $request->getAction(); $updater = new Updater(); if ($cur_stage >= STAGE_ONLINE && $_SESSION['logged_in'] == 1 && $updater->updateAvailable()) { if ($controller != 'update') { $request->setController('update'); $request->setAction('index'); } } else { if ($cur_stage != STAGE_ONLINE && $_SERVER['SERVER_NAME'] != '192.168.101.1') { header('Location: http://192.168.101.1/' . $controller . '/' . $action); die; } } $page_html = ''; try { $page_html = $dispatcher->run($request); } catch (NotFoundException $e) { if ($cur_stage != STAGE_ONLINE) { header('Location: http://192.168.101.1/index/index'); die; }