Symfony2服务类。函数调用

汤米

我有这样的服务:

  services:
    prodacom_authentication.encode:
      class: Prodacom\AuthenticationBundle\Service\Encode
      arguments: ["@security.context"]

我要在控制器中调用的服务功能:

public function encodePassword() {
    $factory = $this->get('security.encoder_factory');
    $user = new Prodacom\MainBundle\Entity\PdbUser();

    $encoder = $factory->getEncoder($user);
    $password = $encoder->encodePassword('ryanpass', $user->getSalt());
    var_dump($password);
    $user->setPassword($password);
}

我想在authenticationController.php中调用函数encodePassword。

    $this->get('prodacom_authentication.encode')->encodePassword();

但我不断收到此错误:

    Attempted to call method "get" on class "Prodacom\AuthenticationBundle\Service\Encode" in C:\htdocs\domeinbeheer\src\Prodacom\AuthenticationBundle\Service\Encode.php line 12.

有任何想法吗 ???

西蒙·西姆城

问题出在方法的第二行,您在其中调用: $this->get('security.encoder_factory')

在您的服务中,get()如果您尚未实现该方法,则不会定义该方法您的控制器拥有它,因为它扩展了一个名为的类ContainerAware该类实现了此方法。

现在,您可以注入完整的依赖关系容器(不推荐),也可以只将所需的服务注入服务中。以下是可用于将服务注入服务中的不同方式的列表:http : //symfony.com/doc/current/components/dependency_injection/types.html

我看到您已经熟悉构造函数注入...

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章