/** * Dispatch request to Controller * */ public function dispatch(Request $request = NULL, Response $response = NULL) { $request = isset($request) ? $request : new Request(); $response = isset($response) ? $response : new Response(); // Import common module $flag = import('common/*'); // Adjust $_GET['q'] $request->adjust_q(); // Call possible hooks if (method_exists('Common_Controller', 'on_shutdown')) { register_shutdown_function(array('Common_Controller', 'on_shutdown'), $request, $response); } if (method_exists('Common_Controller', 'on_dispatch_before')) { Common_Controller::on_dispatch_before($request, $response); } $module = ''; $action = ''; $q = $request->q(); if ('' == $q) { $q = 'default' . $this->qsep . 'index'; } $qarr = explode($this->qsep, $q); if (count($qarr) > 0) { $module = strtolower($qarr[0]); $action = isset($qarr[1]) ? strtolower($qarr[1]) : 'index'; $moddir = $this->modRootDir() . $module; if (is_dir($moddir)) { $ctlname = ucfirst($module) . '_Controller'; $modfile = $moddir . '/' . $ctlname . '.php'; if (file_exists($modfile)) { // import all class under "$this->modroot/$module/*" import($module, $this->modroot); current_module($module); //save current module // find controller action and dispatch $ctrl = new $ctlname(); $menu = $ctrl->menu(); //hook menu if (count($menu)) { foreach ($menu as $key => $val) { if (self::qMatchPattern($key, $q)) { $action = $val; break; } } } // dispatch if ('' != $action && $ctrl->action_exists($action)) { // hook init $ctrl->init($action, $request, $response); // dispatch action $ctrl->{$action}($request, $response); // tigger dispatch after hook if (method_exists('Common_Controller', 'on_dispatch_after')) { Common_Controller::on_dispatch_after($request, $response); } exit; } } } } throw new NotFoundException(); }