0

我们正在使用 HWIAuhtBundle 进行身份验证,因此有一个这样的用户提供程序接口:

    class OAuthUserProvider implements 
          UserProviderInterface , OAuthAwareUserProviderInterface
    {
      ...
      public function loadUserByUsername($username)
      {
         ...
      }
      public function loadUserByOAuthUserResponse(UserResponseInterface $response)
      {
         ...
      }
   }

有没有办法在 OAuthUserProvider 类的方法中访问 c​​onfig.yml 中指定的参数,例如 facebook 应用程序 ID?

hwi_oauth:
    resource_owners:
        facebook:
            type:                facebook
            client_id:           %fb_app%

或者如何访问ServiceContainer?然后以这种方式获取参数: http://symfony.com/doc/current/components/dependency_injection/parameters.html

4

1 回答 1

0

可以将它作为 OAuthUserProvider 构造函数的参数传递。

1)使用正确的参数名称向服务添加参数。

;services.yml

services:
     my_user.oauth_user_provider: 
         class: %my_user.oauth_user_provider.class%
         arguments: [%facebook_secret%]

2)向类ctor添加一个参数。

//OAuthUserProvider.php

class OAuthUserProvider implements 
      UserProviderInterface , OAuthAwareUserProviderInterface
{
    public function __construct($secret)
    {
       ...
    }
...
}
于 2014-01-18T22:13:52.723 回答