Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony3服务和令牌存储_Symfony_Service - Fatal编程技术网

Symfony3服务和令牌存储

Symfony3服务和令牌存储,symfony,service,Symfony,Service,我尝试设置一个服务来从数据库中检索实体。 My services.yml如下所示: app.twig_global_extension: class: AppBundle\Twig\GlobalExtension arguments: [ "@doctrine.orm.entity_manager", "@security.token_storage" ] tags: - { name: twig.extension } <?php namespa

我尝试设置一个服务来从数据库中检索实体。 My services.yml如下所示:

app.twig_global_extension:
    class: AppBundle\Twig\GlobalExtension
    arguments: [ "@doctrine.orm.entity_manager", "@security.token_storage" ]
    tags:
        - { name: twig.extension }
<?php
namespace AppBundle\Twig;

class GlobalExtension extends \Twig_Extension
{
    protected $em;
    protected $token_storage;

    public function __construct(\Doctrine\ORM\EntityManager $em, \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage $token_storage)
    {
        $this->em = $em;
        $this->token_storage = $token_storage;
    }


    public function getGlobals()
    {
        return [
            'userdata' => $this->em->getRepository('AppBundle:userdata')->findOneBy(['internalid' => $this->token_storage->getToken()->getUser()->getId()]),
        ];
    }

    public function getName()
    {
        return 'app_global_extension';
    }
}
我的紧张情绪是这样的:

app.twig_global_extension:
    class: AppBundle\Twig\GlobalExtension
    arguments: [ "@doctrine.orm.entity_manager", "@security.token_storage" ]
    tags:
        - { name: twig.extension }
<?php
namespace AppBundle\Twig;

class GlobalExtension extends \Twig_Extension
{
    protected $em;
    protected $token_storage;

    public function __construct(\Doctrine\ORM\EntityManager $em, \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage $token_storage)
    {
        $this->em = $em;
        $this->token_storage = $token_storage;
    }


    public function getGlobals()
    {
        return [
            'userdata' => $this->em->getRepository('AppBundle:userdata')->findOneBy(['internalid' => $this->token_storage->getToken()->getUser()->getId()]),
        ];
    }

    public function getName()
    {
        return 'app_global_extension';
    }
}
首先,这个案例的问题是什么

$this->token_storage->getToken()->getUser()->getId()
它检索正确的ID,但在探查器中抛出错误

\Doctrine\ORM\EntityManager,因为它被标记为不推荐使用?

“对非对象(null)调用成员函数getUser()”,因为您的代码是
$this->token\u storage->getToken()->getUser()->getId()
$this->token\u storage->getToken()
必须是
null才能使此错误为真。查看下面的示例,您将看到:

/**
 * Returns the current security token.
 *
 * @return TokenInterface|null A TokenInterface instance or null if no authentication information is available
 */
public function getToken();
换句话说,探查器URL可能不在防火墙后面,因此
getToken()
没有身份验证信息且为空


在您的函数中,请确保考虑到这种情况,例如,当令牌不可用时返回null。

如果“检测”探查器上下文会更干净,否?否,您不知道哪些页面将使用此自定义函数。可能有更多的页面不在防火墙后面。