Пример #1
0
 /**
  * Load a component and dispatch the request to it
  */
 public function dispatch(midgardmvc_core_request $request)
 {
     $route = $request->get_route();
     $argv_str = preg_replace('%/{2,}%', '/', '/' . implode('/', $request->get_arguments()) . '/');
     $query = $request->get_query();
     $arguments = $route->check_match($argv_str, $query);
     if (is_null($arguments)) {
         throw new midgardmvc_exception_notfound('Selected route ' . $route->id . ' doesn\'t match current URL ' . $request->get_path());
     }
     // Initialize controller and pass it the request object
     $controller_class = $route->controller;
     $controller = new $controller_class($request);
     // Define the action method for the route_id
     $request_method = $request->get_method();
     $action_method = "{$request_method}_{$route->action}";
     if ($request_method == 'head') {
         // HEAD is like GET but returns no data
         $action_method = "get_{$route->action}";
     }
     // Run the route and set appropriate data
     try {
         $controller->data = array();
         if (!method_exists($controller, $action_method)) {
             throw new midgardmvc_exception_httperror("{$request_method} method not allowed", 405);
         }
         $controller->{$action_method}($arguments);
     } catch (Exception $e) {
         // Read controller's returned data to context before carrying on with exception handling
         $this->data_to_request($request, $controller->data);
         throw $e;
     }
     $this->data_to_request($request, $controller->data);
 }