If $resource is null, generated route reference will be based on current request's route and parameters.
public generate ( mixed $resource = null, array $params = [] ) : |
||
$resource | mixed | The route name. Can be any resource supported by the different routers (e.g. Location object). |
$params | array | Array of parameters, used to generate the final link along with $resource. |
return |
/** * Used by the REST API to reference downloadable files. * It redirects (permanently) to the standard ez_content_download route, based on the language of the field * passed as an argument, using the language switcher. * * @param mixed $contentId * @param int $fieldId * @param \Symfony\Component\HttpFoundation\Request $request * * @return \Symfony\Component\HttpFoundation\RedirectResponse */ public function redirectToContentDownloadAction($contentId, $fieldId, Request $request) { $content = $this->contentService->loadContent($contentId); $field = $this->findFieldInContent($fieldId, $content); $params = array('content' => $content, 'fieldIdentifier' => $field->fieldDefIdentifier, 'language' => $field->languageCode); if ($request->query->has('version')) { $params['version'] = $request->query->get('version'); } $downloadUrl = $this->router->generate($this->routeReferenceGenerator->generate('ez_content_download', $params)); return new RedirectResponse($downloadUrl, 301); }
/** * @dataProvider generateGenerator */ public function testGenerate($resource, array $params) { $currentRouteName = 'my_route'; $currentRouteParams = array('foo' => 'bar'); $request = new Request(); $request->attributes->set('_route', $currentRouteName); $request->attributes->set('_route_params', $currentRouteParams); $event = new RouteReferenceGenerationEvent(new RouteReference($resource, $params), $request); $this->dispatcher->expects($this->once())->method('dispatch')->with(MVCEvents::ROUTE_REFERENCE_GENERATION, $this->equalTo($event)); $generator = new RouteReferenceGenerator($this->dispatcher); $generator->setRequest($request); $reference = $generator->generate($resource, $params); $this->assertInstanceOf('eZ\\Publish\\Core\\MVC\\Symfony\\Routing\\RouteReference', $reference); $this->assertSame($resource, $reference->getRoute()); $this->assertSame($params, $reference->getParams()); }
public function testGenerateNullResourceWithoutRoute() { $currentRouteName = 'my_route'; $currentRouteParams = array('foo' => 'bar'); $request = new Request(); $requestStack = new RequestStack(); $requestStack->push($request); $event = new RouteReferenceGenerationEvent(new RouteReference(null, array()), $request); $this->dispatcher->expects($this->once())->method('dispatch')->with(MVCEvents::ROUTE_REFERENCE_GENERATION, $this->equalTo($event)); $generator = new RouteReferenceGenerator($this->dispatcher); $generator->setRequestStack($requestStack); $reference = $generator->generate(); $this->assertInstanceOf('eZ\\Publish\\Core\\MVC\\Symfony\\Routing\\RouteReference', $reference); }