dtyyrt4545 2014-04-23 15:37
浏览 52
已采纳

错误:调用成员函数get()

I'm trying to use this provider class to save new user data to database, but I always get this error

"FatalErrorException: Error: Call to a member function get() on a non-object"

The problem is get('security.encoder_factory') is somehow not working. I'm using this to encode the password. Is there any other way or any fix for this code so i can encode the password without getting an error?

this is my provider class code:

namespace xxxx\yyyyBundle\Provider;

use HWI\Bundle\OAuthBundle\Security\Core\User\OAuthUserProvider;
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;'

class Provider extends OAuthUserProvider 
{

//.....
    public function loadUserByOAuthUserResponse(UserResponseInterface $response)
    {
        //.....
        if ( !count($result) ) {

            $user = new User();
            $user->setUsername(xxxxx);
            $encoder = $this->container->get('security.encoder_factory')->getEncoder($user);
            $user->setPassword($encoder->encodePassword(xxxxx, $user->getSalt()));
            $user->setStatus(xxxxx);
            $user->setFID($facebook_id);

            $em = $this->doctrine->getManager();
            $em->persist($user);
            $id = $em->flush();
        } else {
            $id = $result[0]['id'];
        }

        //set id
        $this->session->set('id', $id);


        //parent:: returned value
        return $this->loadUserByUsername($response->getNickname());
    }
}

this exact doctrine code working on the user registration with form.

  • 写回答

1条回答 默认 最新

  • doulang9953 2014-04-23 16:00
    关注

    It looks like you are using HWIOAuthBundle. I don't know this bundle but a cursory glace at its source suggests it's not container aware... Hence your issue: you are trying to call a method on a non-object.

    Since you are extending OAuthUserProvider, you could set up your Provider class as a service itself, inject the container (or better yet just the security.encoder_factory).

    This is not tested but something like:

    <?php 
    
    namespace Foo\BarBundle\Provider;
    
    use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
    use HWI\Bundle\OAuthBundle\Security\Core\User\OAuthUserProvider;
    use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
    
    class BazProvider extends OAuthUserProvider 
    { 
        private $encoder; 
    
        public function __construct(EncoderFactoryInterface $encoder) 
        { 
            // assign injected object as property
            $this->encoder = $encoder; 
        } 
    
        public function loadUserByOAuthUserResponse(UserResponseInterface $response) 
        { 
            $user = new User();
            // use injected object
            $encoder = $this->encoder->getEncoder($user);
            // implement
        } 
    } 
    

    In your bundle's services.yml

    services: 
        baz_provider: 
            class: Foo\BarBundle\Provider\BazProvider 
            arguments: 
                - @security.encoder_factory
    

    Like any service, you call this in any container aware context (such as a controller) like so:

    $bazProvider = $this->get('baz_provider');
    

    Hope this helps :)

    EDIT

    More reading here: http://symfony.com/doc/current/components/dependency_injection/types.html#constructor-injection

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计