/** * Handle the page requests * * @param Request $request The request * @param string $url The url * @param bool $preview Show in preview mode * * @throws NotFoundHttpException * @throws AccessDeniedException * * @return Response|array */ public function slugAction(Request $request, $url = null, $preview = false) { /* @var EntityManager $em */ $em = $this->getDoctrine()->getManager(); $locale = $request->getLocale(); /* @var NodeTranslation $nodeTranslation */ $nodeTranslation = $request->attributes->get('_nodeTranslation'); // If no node translation -> 404 if (!$nodeTranslation) { throw $this->createNotFoundException('No page found for slug ' . $url); } $entity = $this->getPageEntity($request, $preview, $em, $nodeTranslation); $node = $nodeTranslation->getNode(); $securityEvent = new SlugSecurityEvent(); $securityEvent->setNode($node)->setEntity($entity)->setRequest($request)->setNodeTranslation($nodeTranslation); $nodeMenu = $this->container->get('kunstmaan_node.node_menu'); $nodeMenu->setLocale($locale); $nodeMenu->setCurrentNode($node); $nodeMenu->setIncludeOffline($preview); $eventDispatcher = $this->get('event_dispatcher'); $eventDispatcher->dispatch(Events::SLUG_SECURITY, $securityEvent); //render page $renderContext = new RenderContext(array('nodetranslation' => $nodeTranslation, 'slug' => $url, 'page' => $entity, 'resource' => $entity, 'nodemenu' => $nodeMenu)); if (method_exists($entity, 'getDefaultView')) { /** @noinspection PhpUndefinedMethodInspection */ $renderContext->setView($entity->getDefaultView()); } $preEvent = new SlugEvent(null, $renderContext); $eventDispatcher->dispatch(Events::PRE_SLUG_ACTION, $preEvent); $renderContext = $preEvent->getRenderContext(); /** @noinspection PhpUndefinedMethodInspection */ $response = $entity->service($this->container, $request, $renderContext); $postEvent = new SlugEvent($response, $renderContext); $eventDispatcher->dispatch(Events::POST_SLUG_ACTION, $postEvent); $response = $postEvent->getResponse(); $renderContext = $postEvent->getRenderContext(); if ($response instanceof Response) { return $response; } $view = $renderContext->getView(); if (empty($view)) { throw $this->createNotFoundException('No page found for slug ' . $url); } $template = new Template(array()); $template->setTemplate($view); $request->attributes->set('_template', $template); return $renderContext->getArrayCopy(); }
/** * @param GetResponseForControllerResultEvent $event */ public function onKernelView(GetResponseForControllerResultEvent $event) { $response = $event->getControllerResult(); if ($response instanceof Response) { // If it's a response, just continue return; } $request = $event->getRequest(); if ($request->attributes->has('_template')) { //template is already set return; } $nodeTranslation = $request->attributes->get('_nodeTranslation'); if ($nodeTranslation) { $entity = $request->attributes->get('_entity'); $url = $request->attributes->get('url'); $nodeMenu = $request->attributes->get('_nodeMenu'); $parameters = $request->attributes->get('_renderContext'); if ($request->get('preview') === true) { $version = $request->get('version'); if (!empty($version) && is_numeric($version)) { $nodeVersion = $this->em->getRepository('KunstmaanNodeBundle:NodeVersion')->find($version); if (!is_null($nodeVersion)) { $entity = $nodeVersion->getRef($this->em); } } } $renderContext = array('nodetranslation' => $nodeTranslation, 'slug' => $url, 'page' => $entity, 'resource' => $entity, 'nodemenu' => $nodeMenu); if (is_array($parameters) || $parameters instanceof \ArrayObject) { $parameters = array_merge($renderContext, (array) $parameters); } else { $parameters = $renderContext; } if (is_array($response)) { // If the response is an array, merge with rendercontext $parameters = array_merge($parameters, $response); } //set the rendercontext with all params as response, plus the template in the request attribs //the SensioFrameworkExtraBundle kernel.view will handle everything else $event->setControllerResult((array) $parameters); $template = new Template(array()); $template->setTemplate($entity->getDefaultView()); $request->attributes->set('_template', $template); } }
/** * @param Request $request * @param Template $template * @param object $controller * @param string $action * * @return array */ private function resolveDefaultParameters(Request $request, Template $template, $controller, $action) { $parameters = array(); $arguments = $template->getVars(); if (0 === count($arguments)) { $r = new \ReflectionObject($controller); $arguments = array(); foreach ($r->getMethod($action)->getParameters() as $param) { $arguments[] = $param->getName(); } } // fetch the arguments of @Template.vars or everything if desired // and assign them to the designated template foreach ($arguments as $argument) { $parameters[$argument] = $request->attributes->get($argument); } return $parameters; }
private function resolveDefaultParameters(Request $request, Template $template, $controller, $action) { $parameters = array(); $arguments = $template->getVars(); if (0 === count($arguments)) { $r = new \ReflectionObject($controller); $arguments = array(); foreach ($r->getMethod($action)->getParameters() as $param) { $arguments[] = $param->getName(); } } foreach ($arguments as $argument) { $parameters[$argument] = $request->attributes->get($argument); } return $parameters; }