如何在 Symfony 2 的类中获取 Request 对象? [英] How can I get Request object inside a class in Symfony 2?
问题描述
我在 Symfony 中有一个实现接口的类.我需要有 $request 才能有 POST 参数.这是我的功能:
I have a class in Symfony that implements an interface. I need to have $request to have POST params. This is my function:
class WebserviceUserProvider implements UserProviderInterface
{
public function loadUserByUsername($username)
{
$salt = "";
$roles = "";
// make a call to your webservice here
.....
}
...
}
我不能这样做:
public function loadUserByUsername($username, Request $request)
因为我需要实现接口,我得到这个错误:
because i need to implement the interface, and i get this error:
FatalErrorException:编译错误:声明Actas\Gestion\UserBundle\Security\User\WebserviceUserProvider::loadUserByUsername()必须兼容Symfony\Component\Security\Core\User\UserProviderInterface::loadUserByUsername($username)
FatalErrorException: Compile Error: Declaration of Actas\Gestion\UserBundle\Security\User\WebserviceUserProvider::loadUserByUsername() must be compatible with Symfony\Component\Security\Core\User\UserProviderInterface::loadUserByUsername($username)
如何获取请求参数?这个类是从登录调用的,我需要它发送的密码才能使用 WebService 对用户进行身份验证.
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.
在此先非常感谢您!
这是我在 Bundle 中的 services.xml:
This is my services.xml in the Bundle:
# 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"]]
推荐答案
除了 Czechnology 的回答,您还可以使用 setter 方法注入请求.在 services.yml 添加:
In addition to Czechnology's answer, you could also inject the request using a setter method. In services.yml add:
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');
}
// ...
}
这篇关于如何在 Symfony 2 的类中获取 Request 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!