【问题标题】:How to access config.yml parameters in OAuthAwareUserProviderInterface如何访问 OAuthAwareUserProviderInterface 中的 config.yml 参数
【发布时间】:2014-01-16 19:47:17
【问题描述】:

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

    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

【问题讨论】:

    标签: php symfony parameters config hwioauthbundle


    【解决方案1】:

    可以将其作为 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)
        {
           ...
        }
    ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-24
      • 2014-09-05
      • 2016-09-18
      • 2021-03-26
      • 2015-04-18
      • 2020-06-17
      • 2022-12-18
      • 2021-01-11
      相关资源
      最近更新 更多