如何在symfony2全局帮助器功能(服务)中访问服务容器? [英] How to access service container in symfony2 global helper function (service)?

查看:71
本文介绍了如何在symfony2全局帮助器功能(服务)中访问服务容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题始于我不理解为什么我不能将变量传递给symfony2全局帮助器函数(服务)的原因,但是由于人们比我聪明,我意识到我的错误是试图从一个内部使用security_context.没有注入的类,所以...

This question started out with me not understanding why I couldn't pass variables to a symfony2 global helper function (service), but thanks to people brighter than I, I realized my error was about trying to use the security_context from within a class that didn't have it injected so...

这是最终结果,有效的代码.我没有找到更好的方法来对社区有所帮助.

This is the final result, the code that works. I found no better way of making this helpful to the comunity.

这是从symfony2的全局函数或帮助函数中从security_context获取用户和其他数据的方法.

This is how you can get the user and other data from security_context from within a global function or helper function in symfony2.

我具有以下类和功能:

<?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
    }     
}

...被定义为这样的服务(在app/config/config.yml中)...

...defined as a service (in app/config/config.yml) like this...

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

现在,在我的控制器中,我像这样调用此函数...

Now, in my controller I call on this function like this...

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);

推荐答案

我假定在添加属性和构造函数之前发生了第一个错误(未定义的属性).然后,您得到了第二个错误.另一个错误意味着您的构造函数希望接收一个Container对象,但什么也没收到.这是因为定义服务时,您没有告诉依赖注入管理器您想要获取容器.将您的服务定义更改为此:

I assume that the first error (undefined property) happened before you added the property and the constructor. Then you got the second error. This other error means that your constructor expects to receive a Container object but it received nothing. This is because when you defined your service, you did not tell the Dependency Injection manager that you wanted to get the container. Change your service definition to this:

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

然后,构造函数应该期望一个类型为Symfony \ Component \ DependencyInjection \ ContainerInterface的对象;

The constructor should then expect an object of type Symfony\Component\DependencyInjection\ContainerInterface;

use Symfony\Component\DependencyInjection\ContainerInterface as Container;

class globalHelper {    

    private $container;

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

这篇关于如何在symfony2全局帮助器功能(服务)中访问服务容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆