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
.....
}
...
}
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)
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.
Thank you very much in advance!
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"]]
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
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');
}
// ...
}
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. 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.
/**
* 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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.