Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

php - How to access service container in symfony2 global helper function (service)?

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.

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

I have the following class and function:

<?php
namespace BizTVCommonBundleHelper;

use SymfonyComponentDependencyInjectionContainerInterface 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
    }     
}

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

#Registering my global helper functions            
services:
  biztv.helper.globalHelper:
    class: BizTVCommonBundleHelperglobalHelper
    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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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: BizTVCommonBundleHelperglobalHelper
    arguments: ['@service_container']

The constructor should then expect an object of type SymfonyComponentDependencyInjectionContainerInterface;

use SymfonyComponentDependencyInjectionContainerInterface as Container;

class globalHelper {    

    private $container;

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...