protected function createRoute($name, $path) { // split path in static and variable part preg_match('{^(.*?)(/[^/]*\\{.*)?$}', $path, $paths); $route = new Route(); $route->setName($name); $route->setStaticPrefix($paths[1]); if (isset($paths[2])) { $route->setVariablePattern($paths[2]); } $this->getDm()->persist($route); $this->getDm()->flush(); return $route; }
/** * @param Node $node * @param bool $nonConflict * * @return Route */ public function build(Node $node, $nonConflict = true) { $class = get_class($node); $routeMapping = $this->nodeRouteManager->getNodeMapping($class); $router = $this->nodeRouteManager->getRouter(); $i = 0; $path = null; $fullPath = $node->getPath(); $route = new Route(); $route->setVariablePattern(""); if ($fullPath) { $urlVars = []; $route->setStaticPrefix($fullPath); } else { $route->setStaticPrefix($routeMapping['path']); $compiledRoute = $route->compile(); $urlVars = $compiledRoute->getPathVariables(); } $routeName = 'node_route'; $route->setName($routeName); $routeCollection = new RouteCollection(); $routeCollection->add($routeName, $route); $urlGenerator = new UrlGenerator($routeCollection, new RequestContext('')); $urlVarCount = count($urlVars) - 1; while (true) { $params = []; foreach ($urlVars as $vi => $var) { $method = new \ReflectionMethod($class, "get{$var}"); $value = $method->invoke($node); // if ($i > 0 && $urlVarCount == $vi) { $value .= " {$i}"; } $params[$var] = $this->slugger->slugify($value); } if ($fullPath) { $path = $this->slugify($fullPath, $i); } else { $path = $urlGenerator->generate($routeName, $params); } // if we don't care about conflicts, skip if (!$nonConflict) { break; } ++$i; try { $match = $router->match($path); // if we've fallen back to the original route, stop searching for an existing route, or if we've found // the node's route if (isset($match['nodeId'])) { if ($match['nodeId'] != $node->getId()) { continue; } else { break; } } else { break; } } catch (ResourceNotFoundException $e) { break; } } $route = new Route(); $route->setDefaults(['_controller' => 'Gravity\\CmsBundle\\Controller\\NodeController::viewAction', 'nodeId' => $node->getId(), 'type' => $class]); $route->setVariablePattern(""); $route->setStaticPrefix($path); $route->setName($this->buildRouteName($route)); $node->setPath($path); return $route; }