eZ\Publish\Core\MVC\Symfony\Routing\UrlAliasRouter::generate PHP Method

generate() public method

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 also: UrlAliasRouter::supports()
public generate ( string | eZ\Publish\API\Repository\Values\Content\Location $name, mixed $parameters = [], integer $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH ) : string
$name string | eZ\Publish\API\Repository\Values\Content\Location The name of the route or a Location instance
$parameters mixed An array of parameters
$referenceType integer The type of reference to be generated (one of the constants)
return string The generated URL
    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');
    }

Usage Example

Example #1
0
 /**
  * Converts internal links (ezcontent:// and ezlocation://) to URLs.
  *
  * @param \DOMDocument $document
  *
  * @return \DOMDocument
  */
 public function convert(DOMDocument $document)
 {
     $document = clone $document;
     $xpath = new DOMXPath($document);
     $xpath->registerNamespace("docbook", "http://docbook.org/ns/docbook");
     $linkAttributeExpression = "starts-with( @xlink:href, 'ezlocation://' ) or starts-with( @xlink:href, 'ezcontent://' )";
     $xpathExpression = "//docbook:link[{$linkAttributeExpression}]|//docbook:ezlink";
     /** @var \DOMElement $link */
     foreach ($xpath->query($xpathExpression) as $link) {
         // Set resolved href to number character as a default if it can't be resolved
         $hrefResolved = "#";
         $href = $link->getAttribute("xlink:href");
         $location = null;
         preg_match("~^(.+://)?([^#]*)?(#.*|\\s*)?\$~", $href, $matches);
         list(, $scheme, $id, $fragment) = $matches;
         if ($scheme === "ezcontent://") {
             try {
                 $contentInfo = $this->contentService->loadContentInfo($id);
                 $location = $this->locationService->loadLocation($contentInfo->mainLocationId);
                 $hrefResolved = $this->urlAliasRouter->generate($location) . $fragment;
             } catch (APINotFoundException $e) {
                 if ($this->logger) {
                     $this->logger->warning("While generating links for richtext, could not locate " . "Content object with ID " . $id);
                 }
             } catch (APIUnauthorizedException $e) {
                 if ($this->logger) {
                     $this->logger->notice("While generating links for richtext, unauthorized to load " . "Content object with ID " . $id);
                 }
             }
         } else {
             if ($scheme === "ezlocation://") {
                 try {
                     $location = $this->locationService->loadLocation($id);
                     $hrefResolved = $this->urlAliasRouter->generate($location) . $fragment;
                 } catch (APINotFoundException $e) {
                     if ($this->logger) {
                         $this->logger->warning("While generating links for richtext, could not locate " . "Location with ID " . $id);
                     }
                 } catch (APIUnauthorizedException $e) {
                     if ($this->logger) {
                         $this->logger->notice("While generating links for richtext, unauthorized to load " . "Location with ID " . $id);
                     }
                 }
             } else {
                 $hrefResolved = $href;
             }
         }
         $hrefAttributeName = "xlink:href";
         // For embeds set the resolved href to the separate attribute
         // Original href needs to be preserved in order to generate link parameters
         // This will need to change with introduction of UrlService and removal of URL link
         // resolving in external storage
         if ($link->localName === "ezlink") {
             $hrefAttributeName = "href_resolved";
         }
         $link->setAttribute($hrefAttributeName, $hrefResolved);
     }
     return $document;
 }
All Usage Examples Of eZ\Publish\Core\MVC\Symfony\Routing\UrlAliasRouter::generate