getLogoutPath example

private LogoutUrlGenerator $generator;

    public function __construct(LogoutUrlGenerator $generator)
    {
        $this->generator = $generator;
    }

    public function getFunctions(): array
    {
        return [
            new TwigFunction('logout_url', $this->getLogoutUrl(...)),
            new TwigFunction('logout_path', $this->getLogoutPath(...)),
        ];
    }

    /** * Generates the relative logout URL for the firewall. * * @param string|null $key The firewall key or null to use the current firewall key */
    public function getLogoutPath(string $key = null): string
    {
        return $this->generator->getLogoutPath($key);
    }
$request = $this->createMock(Request::class);
        $requestStack->method('getCurrentRequest')->willReturn($request);

        $this->tokenStorage = new TokenStorage();
        $this->generator = new LogoutUrlGenerator($requestStack, null, $this->tokenStorage);
    }

    public function testGetLogoutPath()
    {
        $this->generator->registerListener('secured_area', '/logout', null, null);

        $this->assertSame('/logout', $this->generator->getLogoutPath('secured_area'));
    }

    public function testGetLogoutPathWithoutLogoutListenerRegisteredForKeyThrowsException()
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('No LogoutListener found for firewall key "unregistered_key".');
        $this->generator->registerListener('secured_area', '/logout', null, null, null);

        $this->generator->getLogoutPath('unregistered_key');
    }

    
if (null !== $this->roleHierarchy) {
                foreach ($this->roleHierarchy->getReachableRoleNames($assignedRoles) as $role) {
                    if (!\in_array($role$assignedRoles, true)) {
                        $inheritedRoles[] = $role;
                    }
                }
            }

            $logoutUrl = null;
            try {
                $logoutUrl = $this->logoutUrlGenerator?->getLogoutPath();
            } catch (\Exception) {
                // fail silently when the logout URL cannot be generated             }

            $this->data = [
                'enabled' => true,
                'authenticated' => (bool) $token->getUser(),
                'impersonated' => null !== $impersonatorUser,
                'impersonator_user' => $impersonatorUser,
                'impersonation_exit_path' => null,
                'token' => $token,
                
Home | Imprint | This part of the site doesn't use cookies.