示例#1
0
 /**
  *
  * Decide on a route to use and dispatch our module's controller action.
  *
  * @param RequestInterface $request
  * @param ResponseInterface $response
  * @return Response
  * @throws \Exception
  */
 public function dispatch(RequestInterface $request, ResponseInterface $response)
 {
     if (false === $this->booted) {
         $this->boot();
     }
     // Routing
     $routeParams = $this->handleRouting($request);
     $request->attributes->add($routeParams);
     // Resolve our Controller
     $resolver = $this->serviceManager->get('ControllerResolver');
     if (false === ($controller = $resolver->getController($request))) {
         throw new NotFoundHttpException(sprintf('Unable to find the controller for path "%s".', $request->getPathInfo()));
     }
     $controllerArguments = $resolver->getArguments($request, $controller);
     $result = call_user_func_array($controller, $controllerArguments);
     if (is_string($result)) {
         $response->setContent($result);
     } else {
         if ($result instanceof ResponseInterface) {
             $response = $result;
         } else {
             throw new \Exception('Invalid response type returned from controller');
         }
     }
     $this->response = $response;
     return $response;
 }