/** * Transfer the process to other action or controller * * @param string $action Action key you want to transfer * @param string $controller Controller key you want to transfer * @return void */ public function transfer($action, $controller = null) { if ($controller != null) { $this->view->setRenderingController($controller); } $this->view->setRenderingAction($action); $actionMethod = NameManager::convertActionToMethod($action); if ($controller == null) { $this->{$actionMethod}(); } else { $subdir = ''; $parts = explode('/', trim($controller, '/')); $partsCnt = count($parts); if ($partsCnt > 1) { $controller = $parts[$partsCnt - 1]; unset($parts[$partsCnt - 1]); $subdir = implode('/', $parts); } $className = NameManager::convertControllerToClass($controller); $ins = Loader::getControllerInstance($className, $subdir); $ins->setRequest($this->request); $ins->setView($this->view); $ins->{$actionMethod}(); } $this->dispatched = true; }
/** * 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; } } }