8000 [TwigBridge] Expose current route in `AppVariable` · symfony/symfony@67aae53 · GitHub
[go: up one dir, main page]

Skip to content

Commit 67aae53

Browse files
committed
[TwigBridge] Expose current route in AppVariable
1 parent 5f55af8 commit 67aae53

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

src/Symfony/Bridge/Twig/AppVariable.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,25 @@ public function getFlashes(string|array $types = null): array
158158

159159
return $result;
160160
}
161+
162+
public function getCurrent_Route(): ?string
163+
{
164+
if (!isset($this->requestStack)) {
165+
throw new \RuntimeException('The "app.current_route" variable is not available.');
166+
}
167+
168+
return $this->getRequest()?->attributes->get('_route');
169+
}
170+
171+
/**
172+
* @return array<string, mixed>
173+
*/
174+
public function getCurrent_Route_Parameters(): array
175+
{
176+
if (!isset($this->requestStack)) {
177+
throw new \RuntimeException('The "app.current_route_parameters" variable is not available.');
178+
}
179+
180+
return $this->getRequest()?->attributes->get('_route_params') ?? [];
181+
}
161182
}

src/Symfony/Bridge/Twig/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* Add `form_label_content` and `form_help_content` block to form themes
88
* Add `#[Template()]` to describe how to render arrays returned by controllers
99
* Add support for toggle buttons in Bootstrap 5 form theme
10+
* Add `app.current_route` and `app.current_route_parameters` variables
1011

1112
6.1
1213
---

src/Symfony/Bridge/Twig/Tests/AppVariableTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,40 @@ public function testGetFlashes()
228228
);
229229
}
230230

231+
public function testGetCurrentRoute()
232+
{
233+
$this->setRequestStack(new Request(attributes: ['_route' => 'some_route']));
234+
235+
$this->assertSame('some_route', $this->appVariable->getCurrent_Route());
236+
}
237+
238+
public function testGetCurrentRouteWithRequestStackNotSet()
239+
{
240+
$this->expectException(\RuntimeException::class);
241+
$this->appVariable->getCurrent_Route();
242+
}
243+
244+
public function testGetCurrentRouteParameters()
245+
{
246+
$routeParams = ['some_param' => true];
247+
$this->setRequestStack(new Request(attributes: ['_route_params' => $routeParams]));
248+
249+
$this->assertSame($routeParams, $this->appVariable->getCurrent_Route_Parameters());
250+
}
251+
252+
public function testGetCurrentRouteParametersWithoutAttribute()
253+
{
254+
$this->setRequestStack(new Request());
255+
256+
$this->assertSame([], $this->appVariable->getCurrent_Route_Parameters());
257+
}
258+
259+
public function testGetCurrentRouteParametersWithRequestStackNotSet()
260+
{
261+
$this->expectException(\RuntimeException::class);
262+
$this->appVariable->getCurrent_Route_Parameters();
263+
}
264+
231265
protected function setRequestStack($request)
232266
{
233267
$requestStackMock = $this->createMock(RequestStack::class);

0 commit comments

Comments
 (0)
0