Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ee27ed8

Browse filesBrowse files
committed
added an absolute_url() Twig function
1 parent ea1ac32 commit ee27ed8
Copy full SHA for ee27ed8

File tree

Expand file treeCollapse file tree

4 files changed

+84
-0
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+84
-0
lines changed

‎src/Symfony/Bridge/Twig/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
2.7.0
5+
-----
6+
7+
* added an HttpFoundation extension (provides the `absolute_url` function)
8+
49
2.5.0
510
-----
611

+72Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Twig\Extension;
13+
14+
use Symfony\Component\HttpFoundation\RequestStack;
15+
use Symfony\Component\Asset\Packages;
16+
17+
/**
18+
* Twig extension for the Symfony HttpFoundation component.
19+
*
20+
* @author Fabien Potencier <fabien@symfony.com>
21+
*/
22+
class HttpFoundationExtension extends \Twig_Extension
23+
{
24+
private $requestStack;
25+
26+
public function __construct(RequestStack $requestStack)
27+
{
28+
$this->requestStack = $requestStack;
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function getFunctions()
35+
{
36+
return array(
37+
new \Twig_SimpleFunction('absolute_url', array($this, 'generateAbsoluteUrl')),
38+
);
39+
}
40+
41+
/**
42+
* Returns the absolute URL for the given path.
43+
*
44+
* This method returns the path unchanged if no request is available.
45+
*
46+
* @param string $path The path
47+
*
48+
* @return string The absolute URL
49+
*/
50+
public function generateAbsoluteUrl($path)
51+
{
52+
if (false !== strpos($path, '://') || '//' === substr($path, 0, 2)) {
53+
return $path;
54+
}
55+
56+
if (!$request = $this->requestStack->getMasterRequest()) {
57+
return $path;
58+
}
59+
60+
return $request->getUriForPath($path);
61+
}
62+
63+
/**
64+
* Returns the name of the extension.
65+
*
66+
* @return string The extension name
67+
*/
68+
public function getName()
69+
{
70+
return 'request';
71+
}
72+
}

‎src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExtensionPass.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExtensionPass.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,9 @@ public function process(ContainerBuilder $container)
4242
if ($container->has('fragment.handler')) {
4343
$container->getDefinition('twig.extension.httpkernel')->addTag('twig.extension');
4444
}
45+
46+
if ($container->has('request_stack')) {
47+
$container->getDefinition('twig.extension.httpfoundation')->addTag('twig.extension');
48+
}
4549
}
4650
}

‎src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@
103103
<argument type="service" id="fragment.handler" />
104104
</service>
105105

106+
<service id="twig.extension.httpfoundation" class="Symfony\Bridge\Twig\Extension\HttpFoundationExtension" public="false">
107+
<argument type="service" id="request_stack" />
108+
</service>
106109

107110
<service id="twig.extension.form" class="%twig.extension.form.class%" public="false">
108111
<argument type="service" id="twig.form.renderer" />

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.