/** * @param string $routeName * @param array $arguments * @throws ForwardException */ public function forwardToRoute($routeName, array $arguments = []) { $forwardException = new ForwardException(); $forwardException->setPath($this->urlGenerator->generate($routeName, $arguments)); throw $forwardException; }
/** * @return Server */ public function getServer() { return new Server(function (ServerRequestInterface $request, ResponseInterface $response) { $dispatch = function (ServerRequestInterface $request, ResponseInterface $response) { $route = $this->getRouteMatcher()->match($request); foreach ($route->attributes as $key => $value) { $request = $request->withAttribute($key, $value); } if ($route === false) { $forwardException = new ForwardException(); $forwardException->setPath('/404'); throw $forwardException; } $handlerClassname = $route->handler; if (!is_string($handlerClassname) || !class_exists($handlerClassname)) { throw new \Exception('You have to provide proper classnames as handlers in your routes', 1454170067); } $handler = new $handlerClassname(); if (!$handler instanceof ControllerInterface) { throw new \Exception('Handler has to implement the ControllerInterface ', 1454175394); } $handler->setConfiguration(isset($this->getConfiguration()['app']) ? $this->getConfiguration()['app'] : []); $handler->setEntityManager($this->getEntityManager()); $view = new View(explode('.', $route->name, 2)[0], $this->getTwigEnvironment()); $handler->setView($view); if (!method_exists($handler, $request->getMethod())) { throw new \Exception('Method ' . $request->getMethod() . ' not supported by handler ' . $handlerClassname, 1454170178); } if (method_exists($handler, 'setAuthenticationService')) { $handler->setAuthenticationService($this->getAuthenticationService()); } if (method_exists($handler, 'setCsrfTokenManager')) { $handler->setCsrfTokenManager($this->getCsrfTokenManager()); } if (method_exists($handler, 'setMailService')) { $handler->setMailService($this->serviceContainer->getSingleton(MailService::class, $this->getConfiguration()->get('swiftmailer'))); } if (method_exists($handler, 'setUrlGenerator')) { $handler->setUrlGenerator($this->getUrlGenerator()); } if (method_exists($handler, 'initializeAction')) { $handler->initializeAction(); } $returned = call_user_func([$handler, $request->getMethod()], $request, $response); if ($returned) { $response->getBody()->write($returned); } else { $response->getBody()->write($handler->render()); } }; for ($i = 0; $i < 23; $i++) { try { $dispatch($request, $response); break; } catch (ForwardException $e) { $request = $this->getRequest($e->getPath(), 'get'); } } $this->getEntityManager()->flush(); }, $this->getRequest(), $this->serviceContainer->getSingleton(Response::class)); }
/** * @param string $path * @throws ForwardException */ public function forward($path) { $forwardException = new ForwardException(); $forwardException->setPath($path); throw $forwardException; }