【问题标题】:Change service parameter value dynamically for FOSUserBundle为 FOSUserBundle 动态更改服务参数值
【发布时间】:2013-03-06 07:15:57
【问题描述】:

我之前询问过using multiple entity manager for FOSUserBundle,结果发现 FOSUserBundle (部分)支持它。我需要做的就是在model_manager_name 参数中指定我要使用的连接/管理器,如here 解释的那样

fos_user:
# ........
    model_manager_name: account1

示例应用程序/config/config.yml

有了这个 FOSUserBundle 将使用account1 连接,并使用该连接数据库中的用户信息。

doctrine:
dbal:
    default_connection:       default
    connections:
    account2:
        dbname:           account2
        user:             account2
        password:         password2
        driver:   pdo_mysql
        host:     localhost
        port:     ~
        charset:  UTF8
    account1:
        dbname:           account1
        user:             account1
        password:         password1
        driver:   pdo_mysql
        host:     localhost
        port:     ~
        charset:  UTF8
    default:
        dbname:           account
        user:             account
        password:         password
        driver:   pdo_mysql
        host:     localhost
        port:     ~
        charset:  UTF8

我的应用要求当用户访问(例如)http://myapp.com/a/account1 时,应用将使用account1 连接,而访问http://myapp.com/a/account2 将使用account2 的连接。对于我的应用程序的逻辑,这很容易从我的控制器中完成,因为我可以使用以下内容;

$em = $this->get('doctrine')->getManager('account2');
$repository = $this->get('doctrine')->getRepository($class, 'account2')

但对于登录部分,这并不容易。 FOSUserBundle 作为服务容器运行,我不知道在哪里/如何动态更改 model_manager_name 的值。我确实知道在FOS\UserBundle\DependencyInjection\FOSUserExtension 中我可以通过以下方式手动更改其值;

class FOSUserExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
    $processor = new Processor();
    $configuration = new Configuration();

    $config = $processor->processConfiguration($configuration, $configs);

    $config['model_manager_name'] = 'account2';
    // .................

有什么想法吗?

【问题讨论】:

    标签: symfony dependency-injection fosuserbundle


    【解决方案1】:

    配置存储在容器内,确切地说是在fos_user.model_manager_name

    您可以编写编译器通行证。这将在冻结容器之前执行,这是您可以更改容器的最后一个地方,并且是根据其他服务更改容器的地方。

    您的编译器传递将如下所示:

    // src/Acme/DemoBundle/DependencyInjection/Compiler/ChangeModelManagerPass.php
    namespace Acme\DemoBundle\DependencyInjection\Compiler;
    
    use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
    use Symfony\Component\DependencyInjection\ContainerBuilder;
    
    class ChangeModelManagerPass implements CompilerPassInterface
    {
        public function process(ContainerBuilder $container)
        {
            $request = $container->get('request');
            $uri = $request->getUri();
    
            // check if the uri matches some pattern which will cause a change in the
            // `model_manager_name` setting
            if (...) {
                // ... do some stuff to get the correct model manager name
    
                // set the setting
                $container->setParameter('fos_user.model_manager_name', ...);
            }
        }
    }
    

    the docs 或 Richard Miller 的 this great blog post 中阅读有关编译器传递的更多信息。

    【讨论】:

    • 感谢您向我介绍编译器通行证!但是,由于出现此错误,我无法访问“请求”服务; RuntimeException: You have requested a synthetic service ("request"). The DIC does not know how to construct this service.有什么想法吗?
    • @MohdShakirZakaria 您无法在request 范围之外访问request 服务。我其实不知道如何解决这个问题...... :(
    • 使用request_stack服务
    猜你喜欢
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多