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)"} []
编辑2:
因此,使用给定的错误日志,这就是我所做的更改,并且现在运行良好。
public function getGlobals()
{
$globalVars = [];
if (null !== $token = $this->tokenStorage->getToken()) {
$globalVars['user'] = $token->getUser();
}
return $globalVars;
}
答案 0 :(得分:1)
首先,您不应将整个容器注入扩展程序中。
始终尝试仅注入您需要的东西。在您的示例中,您可以直接注入AuthorizationCheckerInterface
而不是ContainerInterface
。
关于您的错误,没有日志,猜测有点麻烦,但是您应该尝试在调用getToken()
之前检查null
方法是否未返回getUser()
。
<?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;
}
}