[英]Twig GlobalsInterface breaks Symfony Debug Toolbar
Symfony 4.3.1
我有一個Twig-Extension,在其中向Twig全局添加變量。 一旦在類中實現了Twig\\Extension\\GlobalsInterface
,Symfony調試工具欄就會呈現“加載Web調試工具欄時發生錯誤”。 變量可以完美地添加到全局范圍中。
這是我的擴展名:
<?php
namespace App\Twig;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Doctrine\ORM\EntityManagerInterface;
use Twig\Extension\AbstractExtension;
use Twig\Extension\GlobalsInterface;
class GlobalVarsExtension extends AbstractExtension implements GlobalsInterface
{
protected $em;
protected $tokenStorage;
protected $authorizationChecker;
public function __construct(EntityManagerInterface $em, AuthorizationCheckerInterface $authorizationChecker, TokenStorageInterface $tokenStorage)
{
$this->em = $em;
$this->tokenStorage = $tokenStorage;
$this->authorizationChecker = $authorizationChecker;
}
public function getGlobals()
{
$globalVars = [];
if ($this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY')) {
if (null !== $token = $this->tokenStorage->getToken()) {
$globalVars['user'] = $token->getUser();
}
}
return $globalVars;
}
}
這是我實現的接口:
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Extension;
/**
* Enables usage of the deprecated Twig\Extension\AbstractExtension::getGlobals() method.
*
* Explicitly implement this interface if you really need to implement the
* deprecated getGlobals() method in your extensions.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
interface GlobalsInterface
{
/**
* Returns a list of global variables to add to the existing list.
*
* @return array An array of global variables
*/
public function getGlobals();
}
class_alias('Twig\Extension\GlobalsInterface', 'Twig_Extension_GlobalsInterface');
我按照他們的文檔添加了全局變量: https : //twig.symfony.com/doc/2.x/advanced.html#id1
這是來自Twig的變更日志,其中指出該方法已棄用: https : //twig.symfony.com/doc/1.x/deprecated.html#extensions
我想念什么? 還是有其他方法添加全局變量?
編輯:
在意識到我完全錯過了symfony錯誤日志之后,錯誤以完全不同的原因出現了……
錯誤日志:
[2019-06-13 15:49:18] request.CRITICAL: Uncaught PHP Exception Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException: "The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL." at /var/www/rekkt.de/vendor/symfony/security-core/Authorization/AuthorizationChecker.php line 49 {"exception":"[object] (Symfony\\Component\\Security\\Core\\Exception\\AuthenticationCredentialsNotFoundException(code: 0): The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL. at /var/www/rekkt.de/vendor/symfony/security-core/Authorization/AuthorizationChecker.php:49)"} []
[2019-06-13 15:49:18] request.CRITICAL: Exception thrown when handling an exception (Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException: The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL. at /var/www/rekkt.de/vendor/symfony/security-core/Authorization/AuthorizationChecker.php line 49) {"exception":"[object] (Symfony\\Component\\Security\\Core\\Exception\\AuthenticationCredentialsNotFoundException(code: 0): The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL. at /var/www/rekkt.de/vendor/symfony/security-core/Authorization/AuthorizationChecker.php:49)"} []
[2019-06-13 15:49:18] php.CRITICAL: Uncaught Exception: The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL. {"exception":"[object] (Symfony\\Component\\Security\\Core\\Exception\\AuthenticationCredentialsNotFoundException(code: 0): The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL. at /var/www/rekkt.de/vendor/symfony/security-core/Authorization/AuthorizationChecker.php:49, Symfony\\Component\\Security\\Core\\Exception\\AuthenticationCredentialsNotFoundException(code: 0): The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL. at /var/www/rekkt.de/vendor/symfony/security-core/Authorization/AuthorizationChecker.php:49)"} []
[2019-06-13 15:49:18] request.CRITICAL: Uncaught PHP Exception Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException: "The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL." at /var/www/rekkt.de/vendor/symfony/security-core/Authorization/AuthorizationChecker.php line 49 {"exception":"[object] (Symfony\\Component\\Security\\Core\\Exception\\AuthenticationCredentialsNotFoundException(code: 0): The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL. at /var/www/rekkt.de/vendor/symfony/security-core/Authorization/AuthorizationChecker.php:49, Symfony\\Component\\Security\\Core\\Exception\\AuthenticationCredentialsNotFoundException(code: 0): The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL. at /var/www/rekkt.de/vendor/symfony/security-core/Authorization/AuthorizationChecker.php:49)"} []
[2019-06-13 15:49:18] request.CRITICAL: Exception thrown when handling an exception (Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException: The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL. at /var/www/rekkt.de/vendor/symfony/security-core/Authorization/AuthorizationChecker.php line 49) {"exception":"[object] (Symfony\\Component\\Security\\Core\\Exception\\AuthenticationCredentialsNotFoundException(code: 0): The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL. at /var/www/rekkt.de/vendor/symfony/security-core/Authorization/AuthorizationChecker.php:49)"} []
EDIT2:
因此,根據給定的錯誤日志,這就是我所做的更改,並且現在工作良好。
public function getGlobals()
{
$globalVars = [];
if (null !== $token = $this->tokenStorage->getToken()) {
$globalVars['user'] = $token->getUser();
}
return $globalVars;
}
首先,您不應將整個容器注入擴展程序中。
始終嘗試僅注入所需的東西。 在您的示例中,您可以直接注入AuthorizationCheckerInterface
而不是ContainerInterface
。
關於您的錯誤,如果沒有日志,猜測起來有些麻煩,但是您應該嘗試在調用getUser()
之前檢查getToken()
方法是否未返回null
。
<?php
namespace App\Twig;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Doctrine\ORM\EntityManagerInterface;
use Twig\Extension\AbstractExtension;
use Twig\Extension\GlobalsInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class GlobalVarsExtension extends AbstractExtension implements GlobalsInterface
{
protected $em;
protected $tokenStorage;
protected $authorizationChecker;
public function __construct(EntityManagerInterface $em, AuthorizationCheckerInterface $authorizationChecker, TokenStorageInterface $tokenStorage)
{
$this->em = $em;
$this->tokenStorage = $tokenStorage;
$this->authorizationChecker = $authorizationChecker;
}
public function getGlobals()
{
$globalVars = [];
if ($this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY')) {
if (null !== $token = $this->tokenStorage->getToken()) {
$globalVars['user'] = $token->getUser();
}
}
return $globalVars;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.