/** * Forward to other action without redirect * * @param string $uri * @param string $method * * @return Response * @throws BaseException */ public function forward($uri, $method = Request::METHOD_GET) { if ('cli' === PHP_SAPI) { throw new BaseException('You can not call forward method in cli mode.'); } $request = $this->getRequest()->withMethod($method); $this->getContainer()->setShared('request', $request); $oldRouter = $this->getRouter(); $oldRequestUri = $_SERVER['REQUEST_URI']; $oldGetUrl = $_GET['_url']; $_GET['_url'] = $_SERVER['REQUEST_URI'] = $uri; $this->getContainer()->setShared('router', new Router()); $this->getRouter()->setPrefix($oldRouter->getPrefix()); $this->getRouter()->handle($uri); $dispatcher = new Dispatcher(); $response = $dispatcher->setAction($this->getRouter()->getAction())->setActionNamespace($this->getRouter()->getActionNamespace())->setBundle($this->getRouter()->getBundle())->setParams($this->getRouter()->getParams())->setResponderNamespace($this->getRouter()->getResponderNamespace())->setRouteMatched($this->getRouter()->isMatched())->dispatch($request); // restore latest state $this->getContainer()->setShared('router', $oldRouter); $_SERVER['REQUEST_URI'] = $oldRequestUri; $_GET['_url'] = $oldGetUrl; return $response; }
/** * Handle Web Request * * @param ServerRequestInterface $request * * @throws BaseException * @throws DependencyInjection\Exception\ServiceLockedException * @throws DependencyInjection\Exception\ServiceNotFoundException * @throws NotFoundException * @throws Response\Exception */ public function handleWeb(ServerRequestInterface $request) { $this->container->set('request', $request); /** @var Router $router */ $router = $this->container->get('router'); $router->handle(); $dispatcher = new Dispatcher(); $response = $dispatcher->setAction($router->getAction())->setActionNamespace($router->getActionNamespace())->setBundle($router->getBundle())->setParams($router->getParams())->setResponderNamespace($router->getResponderNamespace())->setRouteMatched($router->isMatched())->dispatch($request); if (!$response instanceof Response) { $response = new Response(); } $response->send(); }