/** * 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); }
public function test_methods_valid() { $verbs = array('head', 'get', 'trace', 'options', 'put', 'delete', 'post', 'connect', 'patch', 'propfind', 'proppatch', 'mkcol', 'copy', 'move', 'lock', 'unlock'); $request = new midgardmvc_core_request(); foreach ($verbs as $verb) { $request->set_method(strtoupper($verb)); $this->assertEquals($verb, $request->get_method()); } }
private function dispatch_route(midgardmvc_core_request $request, array $arguments) { $route = $request->get_route(); // Initialize controller and pass it the request object $controller_class = $route->controller; if (!class_exists($controller_class)) { throw new midgardmvc_exception_httperror("Controller class {$controller_class} not found", 500); } $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); }