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

发布于 2024-12-27 19:43:01 字数 3052 浏览 1 评论 0原文

所以基本上,我试图在 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;
    }
}

So basically, I'm trying to implement FOSFacebookBundle with FOSUserBundle in Symfony. I followed the readme for the 2.0 branch of FOSFacebookBundle but right now I'm stuck with this error. Any idea ?

The error :

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 (security section) :

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 }

Relevant parts of 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 (modified so I can use the email address as the username with FOSUserbundle. Don't know if it's really relevant, but the error tells me there's a problem with the second argument passed to the FacebookProvider Constructor, and this argument is supposed to be a 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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眸中客 2025-01-03 19:43:01

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

因此,您应该定义一个自定义用户提供程序此处进行了解释(安装步骤 6. 和“使用 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 相关字段来编辑您的 User 实体(ID、名字、姓氏等)

自述文件末尾的代码。 MD

The FacebookProvider (FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider) implements AuthenticationProviderInterface and not UserProviderInterface. It is not a User provider but an Authentication provider, and fos_facebook needs a User provider.

So you should define a custom User provider : it's explained here (Installation step 6. and in "Example Custom User Provider using the FOS\UserBundle")

1) security.yml

Add this :

# 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

Add this :

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

Create a file FacebookProvider.php in Acme/MyBundle/Security/User/Provider/

Code at the end of Readme.md

4) User.php

Edit your User entity by adding some FB-related fields (Id, first name, last name, etc.)

Code at the end of Readme.md

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文