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 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错