在尝试配置FOSFacebookBundle时,Symfony中出现错误500

时间:2012-01-20 11:57:35

标签: facebook symfony fosuserbundle

基本上,我试图在Symfony中使用FOSUserBundle实现FOSFacebookBundle。我关注了FOSFacebookBundle的2.0分支的自述文件,但是现在我仍然坚持这个错误。有什么想法吗?

错误:

Catchable Fatal Error: Argument 2 passed to FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::__construct()
must implement interface Symfony\Component\Security\Core\User\UserProviderInterface,
instance of FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider given,
called in /var/www/Symfony/app/cache/dev/appDevDebugProjectContainer.php on line 2434
and defined in /var/www/Symfony/vendor/bundles/FOS/FacebookBundle/Security/Authentication/Provider/FacebookProvider.php line 37

security.yml(安全部分):

factories:
    - "%kernel.root_dir%/../vendor/bundles/FOS/FacebookBundle/Resources/config/security_factories.xml"

providers:
    chain_provider:
        providers: [fos_userbundle, fos_facebook]
    fos_userbundle:
        id: fos_user.user_manager
    fos_facebook:
        id: fos_facebook.auth 

encoders:
    "FOS\UserBundle\Model\UserInterface": sha512

firewalls:
    main:
        pattern:        .*
        fos_facebook:
            provider: fos_facebook 
            app_url: "to_do"
            server_url: "to_do"
            default_target_path: /
        form_login:
            provider:       fos_userbundle
            login_path:     /login
            use_forward:    false
            check_path:     /login_check
            failure_path:   null
            default_target_path: /
        logout:
            path:   /logout
            target: /
        anonymous:    true

access_control:
    - { path: /admin/.*, role: ROLE_ADMIN }
    - { path: /video/.*, role: ROLE_USER }
    - { path: /$, role: IS_AUTHENTICATED_ANONYMOUSLY }

config.yml的相关部分:

fos_user:
    db_driver:     orm
    firewall_name: main
    use_listener: false
    user_class:   Projectname\UserBundle\Entity\User
    registration:
        form:
            type: projectname_user_registration
    service:
        user_manager: projectname_user.my_user_manager

fos_facebook:
    file:   %kernel.root_dir%/../vendor/facebook/src/base_facebook.php
    alias:  facebook
    app_id: %projectname_bundle.facebook.app_id%
    secret: %projectname_bundle.facebook.secret%
    cookie: true
    permissions: %projectname_bundle.facebook.perms%

MyUserManager.php(已修改,因此我可以使用电子邮件地址作为FOSUserbundle的用户名。不知道它是否真的相关,但错误告诉我第二个问题传递给FacebookProvider构造函数的参数,该参数应该是UserManager):

<?php

namespace ProjectName\UserBundle\Model;

use FOS\UserBundle\Entity\UserManager;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;

class MyUserManager extends UserManager
{
    public function loadUserByUsername($username)
    {
        $user = $this->findUserByUsernameOrEmail($username);

        if (!$user) {
            throw new UsernameNotFoundException(sprintf('No user with email "%s" was found.', $username));
        }

        return $user;
    }
}

1 个答案:

答案 0 :(得分:0)

FacebookProvider FOS \ FacebookBundle \ Security \ Authentication \ Provider \ FacebookProvider )实现 AuthenticationProviderInterface 而非 UserProviderInterface 。它不是用户提供商,而是身份验证提供商,fos_facebook需要用户提供商。

因此,您应该定义一个自定义用户提供程序:它已解释为here (Installation step 6. and in "Example Custom User Provider using the FOS\UserBundle")

1)security.yml

添加:

# application/config/config.yml
security:
    providers:
        ...
        # choose the provider name freely
        my_fos_facebook_provider:
            id: my.facebook.user
        ...
firewalls:
    main:
        pattern:      .*
        fos_facebook:
            ...
            provider: my_fos_facebook_provider
            ...

2)config.yml

添加:

services:
    my.facebook.user:
        class: Acme\MyBundle\Security\User\Provider\FacebookProvider
        arguments:
            facebook: "@fos_facebook.api"
            userManager: "@fos_user.user_manager"
            validator: "@validator"
            container: "@service_container"

3)FacebookProvider.php

在Acme / MyBundle / Security / User / Provider /

中创建一个文件FacebookProvider.php

Readme.md

末尾的代码

4)User.php

通过添加一些与FB相关的字段(Id,名字,姓氏等)来编辑您的用户实体

Readme.md

末尾的代码