/** * Generates a URL for a location, from the given parameters. * * It is possible to directly pass a Location object as the route name, as the ChainRouter allows it through ChainedRouterInterface. * * If $name is a route name, the "location" key in $parameters must be set to a valid eZ\Publish\API\Repository\Values\Content\Location object. * "locationId" can also be provided. * * If the generator is not able to generate the url, it must throw the RouteNotFoundException * as documented below. * * @see UrlAliasRouter::supports() * * @param string|\eZ\Publish\API\Repository\Values\Content\Location $name The name of the route or a Location instance * @param mixed $parameters An array of parameters * @param int $referenceType The type of reference to be generated (one of the constants) * * @throws \LogicException * @throws \Symfony\Component\Routing\Exception\RouteNotFoundException * @throws \InvalidArgumentException * * @return string The generated URL * * @api */ public function generate($name, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH) { // Direct access to Location if ($name instanceof Location) { return $this->generator->generate($name, $parameters, $referenceType); } // Normal route name if ($name === self::URL_ALIAS_ROUTE_NAME) { if (isset($parameters['location']) || isset($parameters['locationId'])) { // Check if location is a valid Location object if (isset($parameters['location']) && !$parameters['location'] instanceof Location) { throw new LogicException("When generating an UrlAlias route, 'location' parameter must be a valid eZ\\Publish\\API\\Repository\\Values\\Content\\Location."); } $location = isset($parameters['location']) ? $parameters['location'] : $this->locationService->loadLocation($parameters['locationId']); unset($parameters['location'], $parameters['locationId'], $parameters['viewType'], $parameters['layout']); return $this->generator->generate($location, $parameters, $referenceType); } if (isset($parameters['contentId'])) { $contentInfo = $this->contentService->loadContentInfo($parameters['contentId']); unset($parameters['contentId'], $parameters['viewType'], $parameters['layout']); if (empty($contentInfo->mainLocationId)) { throw new LogicException('Cannot generate an UrlAlias route for content without main location.'); } return $this->generator->generate($this->locationService->loadLocation($contentInfo->mainLocationId), $parameters, $referenceType); } throw new InvalidArgumentException("When generating an UrlAlias route, either 'location', 'locationId' or 'contentId' must be provided."); } throw new RouteNotFoundException('Could not match route'); }
/** * Generates a URL for a location, from the given parameters. * * It is possible to directly pass a Location object as the route name, as the ChainRouter allows it through ChainedRouterInterface. * * If $name is a route name, the "location" key in $parameters must be set to a valid eZ\Publish\API\Repository\Values\Content\Location object. * "locationId" can also be provided. * * If the generator is not able to generate the url, it must throw the RouteNotFoundException * as documented below. * * @see UrlAliasRouter::supports() * * @param string|\eZ\Publish\API\Repository\Values\Content\Location $name The name of the route or a Location instance * @param mixed $parameters An array of parameters * @param boolean $absolute Whether to generate an absolute URL * * @throws \LogicException * @throws \Symfony\Component\Routing\Exception\RouteNotFoundException * @throws \InvalidArgumentException * * @return string The generated URL * * @api */ public function generate($name, $parameters = array(), $absolute = false) { // Direct access to Location if ($name instanceof Location) { return $this->generator->generate($name, $parameters, $absolute); } // Normal route name if ($name === self::URL_ALIAS_ROUTE_NAME) { // We must have at least 'location' or 'locationId' to retrieve the UrlAlias if (!isset($parameters['location']) && !isset($parameters['locationId'])) { throw new \InvalidArgumentException("When generating an UrlAlias route, either 'location' or 'locationId must be provided."); } // Check if location is a valid Location object if (isset($parameters['location']) && !$parameters['location'] instanceof Location) { throw new \LogicException("When generating an UrlAlias route, 'location' parameter must be a valid eZ\\Publish\\API\\Repository\\Values\\Content\\Location."); } $location = isset($parameters['location']) ? $parameters['location'] : $this->locationService->loadLocation($parameters['locationId']); unset($parameters['location'], $parameters['locationId'], $parameters['viewType'], $parameters['layout']); return $this->generator->generate($location, $parameters, $absolute); } throw new RouteNotFoundException('Could not match route'); }