add twig function to use path safetly
This commit is contained in:
parent
3226edab34
commit
368fbdbd87
|
@ -29,9 +29,37 @@ class RoutingExtension extends AbstractExtension
|
||||||
new TwigFunction('node_path', [$this, 'getNodePath'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]),
|
new TwigFunction('node_path', [$this, 'getNodePath'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]),
|
||||||
new TwigFunction('safe_node_url', [$this, 'getSafeNodeUrl'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]),
|
new TwigFunction('safe_node_url', [$this, 'getSafeNodeUrl'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]),
|
||||||
new TwigFunction('safe_node_path', [$this, 'getSafeNodePath'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]),
|
new TwigFunction('safe_node_path', [$this, 'getSafeNodePath'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]),
|
||||||
|
new TwigFunction('safe_url', [$this, 'getSafeUrl'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]),
|
||||||
|
new TwigFunction('safe_path', [$this, 'getSafePath'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getSafePath(string $route, array $parameters = [], bool $relative = false): ?string
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return $this->generator->generate(
|
||||||
|
$route,
|
||||||
|
$parameters,
|
||||||
|
$relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH
|
||||||
|
);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSafeUrl(string $route, array $parameters = [], bool $schemeRelative = false): ?string
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return $this->generator->generate(
|
||||||
|
$route,
|
||||||
|
$parameters,
|
||||||
|
$schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL
|
||||||
|
);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function getNodePath(Node $node, array $parameters = [], bool $relative = false): ?string
|
public function getNodePath(Node $node, array $parameters = [], bool $relative = false): ?string
|
||||||
{
|
{
|
||||||
if ($node->hasExternalUrl()) {
|
if ($node->hasExternalUrl()) {
|
||||||
|
|
Loading…
Reference in a new issue