简体   繁体   English

如何在Symfony 2中的类中获取Request对象?

[英]How can I get Request object inside a class in Symfony 2?

I have a class in Symfony that implements an interface. 我在Symfony中有一个实现接口的类。 I need to have $request to have POST params. 我需要有$ para请求POST params。 This is my function: 这是我的功能:

class WebserviceUserProvider implements UserProviderInterface
{
    public function loadUserByUsername($username)
    {
        $salt = "";
        $roles = "";
        // make a call to your webservice here

        .....
    }
...
}

I can't do this: 我不能这样做:

public function loadUserByUsername($username, Request $request)

because i need to implement the interface, and i get this error: 因为我需要实现接口,我得到这个错误:

FatalErrorException: Compile Error: Declaration of Actas\\Gestion\\UserBundle\\Security\\User\\WebserviceUserProvider::loadUserByUsername() must be compatible with Symfony\\Component\\Security\\Core\\User\\UserProviderInterface::loadUserByUsername($username) FatalErrorException:编译错误:Actas \\ Gestion \\ UserBundle \\ Security \\ User \\ WebserviceUserProvider :: loadUserByUsername()声明必须与Symfony \\ Component \\ Security \\ Core \\ User \\ UserProviderInterface :: loadUserByUsername($ username)兼容

How can i get the request params? 我如何获得请求参数? This class is called from login, and i need the password sent by it to use a WebService to authenticate the user. 这个类是从登录调用的,我需要它发送的密码才能使用WebService来验证用户。

Thank you very much in advance! 非常感谢你提前!

This is my services.xml in the Bundle: 这是我在Bundle中的services.xml:

# src/Actas/Gestion/UserBundle/Resources/config/services.yml
parameters:
    webservice_user_provider.class: Actas\Gestion\UserBundle\Security\User\WebserviceUserProvider

services:
    webservice_user_provider:
        class: "%webservice_user_provider.class%"
        scope: container
        calls:
                - [setServiceContainer , ["@service_container"]]
use Symfony\Component\HttpFoundation\RequestStack;

class NewService
{

    protected $request;

    public function setRequest(RequestStack $request_stack)
    {
        $this->request = $request_stack->getCurrentRequest();
    }

}

In Config file 在配置文件中

services:

    new.service:
        class: Acme\DemoBundle\NewService
        calls:
            - [setRequest, [@request_stack]]

Reference : http://symfony.com/blog/new-in-symfony-2-4-the-request-stack 参考: http//symfony.com/blog/new-in-symfony-2-4-the-request-stack

In addition to Czechnology's answer, you could also inject the request using a setter method. 除了Czechnology的答案,您还可以使用setter方法注入请求。 In services.yml add: 在services.yml中添加:

my_service:
    class: Acme\DemoBundle\Service\WebserviceUserProvider
    scope: request
    calls:
        - [setRequest , ["@request"]]

Then declare your class like this: 然后像这样声明你的类:

use Symfony\Component\HttpFoundation\Request;

class WebserviceUserProvider implements UserProviderInterface {
    private $request;
    public function setRequest( Request $request ) {

        $this->request = $request;
    }
    // ...
}

If you have scope widening issues, you could also try injecting the service container and getting the requesting from it. 如果您有范围扩大问题,您还可以尝试注入服务容器并从中获取请求。 In services declare your service like this: 在服务中声明您的服务如下:

my_service:
class: Acme\DemoBundle\Service\WebserviceUserProvider
calls:
    - [setRequest , ["@service_container"]]

Now just use the container to get the request: 现在只需使用容器来获取请求:

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;

class WebserviceUserProvider implements UserProviderInterface {
    private $request;
    public function setRequest( ContainerInterface $container ) {

        $this->request = $container->get('request');
    }
    // ...
}

How do you load the service? 你如何加载服务? You can inject various stuff in the service: 您可以在服务中注入各种内容:

my_service:
    class:      Acme\DemoBundle\Service\WebserviceUserProvider
    arguments:  [@request]
    scope:      request

and use it in the constructor 并在构造函数中使用它

use Symfony\Component\HttpFoundation\Request;

class WebserviceUserProvider implements UserProviderInterface {
    private $request;
    public function __construct(Request $request) {
        $this->request = $request;
    }
    // ...
}

I had a similar problem. 我遇到了类似的问题。 May be it helps this solution. 可能有助于这个解决方案。 You can inject the @service_container to the service. 您可以将@service_container注入服务。 Then you can check if the scope 'request' is active and get then the request. 然后,您可以检查范围“请求”是否处于活动状态,然后获取请求。 This is a service for creating a global variable for accesing the language variables from twig. 这是一个用于创建全局变量的服务,用于从twig访问语言变量。

/**
* Servicio para acceder a los datos del idioma actual de forma global.
*
* @author jaime
*
*/
class IdiomaService {

    private $idioma;

    /**
     * Constructor de la clase.
     */
    public function __construct(Container $container, EntityManager $manager, SecurityContext $context){ //, $request ) {
        if ($container->isScopeActive('request')) {
            $_locale = $container->get('request')->getLocale();
        }else{
            $_locale = 'es';
        }
        $this->idioma = $manager->getRepository ( 'BackBundle:Idioma' )->findOneByCodigo ( $_locale);
    }

    public function getIdioma() {
        return $this->idioma;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM