Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[TwigBridge] Expose current route in AppVariable #47535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/Symfony/Bridge/Twig/AppVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,25 @@ public function getFlashes(string|array $types = null): array

return $result;
}

public function getCurrent_Route(): ?string
{
if (!isset($this->requestStack)) {
throw new \RuntimeException('The "app.current_route" variable is not available.');
}

return $this->getRequest()?->attributes->get('_route');
}

/**
* @return array<string, mixed>
*/
public function getCurrent_Route_Parameters(): array
{
if (!isset($this->requestStack)) {
throw new \RuntimeException('The "app.current_route_parameters" variable is not available.');
}

return $this->getRequest()?->attributes->get('_route_params') ?? [];
}
}
1 change: 1 addition & 0 deletions src/Symfony/Bridge/Twig/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Add `form_label_content` and `form_help_content` block to form themes
* Add `#[Template()]` to describe how to render arrays returned by controllers
* Add support for toggle buttons in Bootstrap 5 form theme
* Add `app.current_route` and `app.current_route_parameters` variables

6.1
---
Expand Down
34 changes: 34 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/AppVariableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,40 @@ public function testGetFlashes()
);
}

public function testGetCurrentRoute()
{
$this->setRequestStack(new Request(attributes: ['_route' => 'some_route']));

$this->assertSame('some_route', $this->appVariable->getCurrent_Route());
}

public function testGetCurrentRouteWithRequestStackNotSet()
{
$this->expectException(\RuntimeException::class);
$this->appVariable->getCurrent_Route();
}

public function testGetCurrentRouteParameters()
{
$routeParams = ['some_param' => true];
$this->setRequestStack(new Request(attributes: ['_route_params' => $routeParams]));

$this->assertSame($routeParams, $this->appVariable->getCurrent_Route_Parameters());
}

public function testGetCurrentRouteParametersWithoutAttribute()
{
$this->setRequestStack(new Request());

$this->assertSame([], $this->appVariable->getCurrent_Route_Parameters());
}

public function testGetCurrentRouteParametersWithRequestStackNotSet()
{
$this->expectException(\RuntimeException::class);
$this->appVariable->getCurrent_Route_Parameters();
}

protected function setRequestStack($request)
{
$requestStackMock = $this->createMock(RequestStack::class);
Expand Down