如何访问服务容器中的Symfony2全球辅助函数(服务)?(How to access servic

2019-06-17 19:11发布

这个问题开始了我不理解为什么我不能传递变量的Symfony2的全球性辅助函数(服务),但由于人的亮度比我,我意识到自己的错误是关于尝试到没一个类中使用security_context从“T有它注入所以...

这是最后的结果,工作的代码。 我发现,使这个有帮助的comunity没有更好的办法。

这是你如何可以在Symfony2中一个全局函数或辅助功能中得到security_context用户和其他数据。

我有以下类和函数:

<?php
namespace BizTV\CommonBundle\Helper;

use Symfony\Component\DependencyInjection\ContainerInterface as Container;

class globalHelper {    

private $container;

public function __construct(Container $container) {
    $this->container = $container;
}   

    //This is a helper function that checks the permission on a single container
    public function hasAccess($container)
    {
        $user = $this->container->get('security.context')->getToken()->getUser();

        //do my stuff
    }     
}

...定义为服务(在应用程序/配置/ config.yml)像这样...

#Registering my global helper functions            
services:
  biztv.helper.globalHelper:
    class: BizTV\CommonBundle\Helper\globalHelper
    arguments: ['@service_container']

现在,在我的控制器我呼吁这样这个功能...

public function createAction($id) {

    //do some stuff, transform $id into $entity of my type...

    //Check if that container is within the company, and if user has access to it.
    $helper = $this->get('biztv.helper.globalHelper');
    $access = $helper->hasAccess($entity);

Answer 1:

我假设你添加的属性和构造函数之前的第一个错误(未定义的属性)发生。 然后你得到了第二个错误。 这其他错误意味着你的构造预计将收到一个Container对象,但没有得到任何。 这是因为当你定义你的服务,你没有告诉你想要得到的容器依赖注入经理。 服务定义改成这样:

services:
  biztv.helper.globalHelper:
    class: BizTV\CommonBundle\Helper\globalHelper
    arguments: ['@service_container']

然后,将构造应该期望类型的Symfony \元器件\ DependencyInjection \ ContainerInterface的对象;

use Symfony\Component\DependencyInjection\ContainerInterface as Container;

class globalHelper {    

    private $container;

    public function __construct(Container $container) {
        $this->container = $container;
    }


Answer 2:

永远奏效,尽管不是在OO的最佳实践的方法

global $kernel;
$assetsManager = $kernel->getContainer()->get('acme_assets.assets_manager');‏


Answer 3:

另一种选择是延长ContainerAware:

use Symfony\Component\DependencyInjection\ContainerAware;

class MyService extends ContainerAware
{
    ....
}

它允许你打电话setContainer在服务声明:

foo.my_service:
    class: Foo\Bundle\Bar\Service\MyService
    calls:
        - [setContainer, [@service_container]]

然后,您可以参考容器为您服务是这样的:

$container = $this->container;


Answer 4:

也许它不是最好的方式,但我做的是我通过容器类,所以我每次我需要它的时候有它。

$helpers = new Helpers();
or
$helpers = new Helpers($this->container);

/* My Class */
class Helpers
{
    private $container;

    public function __construct($container = null) {
        $this->container = $container;
    }
    ...
}

屡试不爽我。



Answer 5:

你不应该注入service_container在您的服务。 在你的榜样,你倒是应该注入老security.context或更近security.token_storage代替。 见例如“避免你的代码成为集装箱上依赖”的部分http://symfony.com/doc/current/components/dependency_injection.html 。

例如:

<?php
namespace BizTV\CommonBundle\Helper;

use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;

class globalHelper {    

    private $securityTokenStorage;

    public function __construct(TokenStorage $securityTokenStorage) {
        $this->securityTokenStorage= $securityTokenStorage;
    }   


    public function hasAccess($container)
    {
        $user = $this->securityTokenStorage->getToken()->getUser();

        //do my stuff
    }     
}

应用程序/配置/ config.yml:

services:
  biztv.helper.globalHelper:
    class: BizTV\CommonBundle\Helper\globalHelper
    arguments: ['@security.token_storage']

控制器:

public function createAction($id) {

    $helper = $this->get('biztv.helper.globalHelper');
    $access = $helper->hasAccess($entity);


文章来源: How to access service container in symfony2 global helper function (service)?