dongyu9850 2017-10-07 14:26
浏览 44
已采纳

Symfony 2.8:编译器传递时的动态容器参数,用于security.yml

I'm facing an issue I can't solve by myself in my Symfony 2.8 API project:

I have some paramter loaded at compiler pass in the DependencyInjection of one of my bundle:

class ParametersCompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        //loads api version from database when container gets built.
        $api_version = $container->get('doctrine')->getRepository('MyAppEntityBundle:Parameter')
            ->findOneBy(['name' => 'api_version']);
        if (!$api_version) {
            throw new MyAppException('api_version couldnt be found in database during container build process.');
        }
        $container->setParameter('api_version', $api_version);
    }
}

I dont want this parameter in paramters_*.yml files, to ensure it's ALWAYS retrieved from db + I use it in security.yml file, such as:

security:
    firewalls:
        api:
            pattern:   ^/api/%api_version%/

How can I achieved that ?

Apparently, $container->setParameter('name', $value); doesnt work if there isnt a parameter in parameter_*.yml file with dummy value in parameters.yml + I need to set these values BEFORE security.yml gets parsed and loaded. I have look into symfony core for hours, spotted where security config is loaded from kernel->boot() process but I dont see how to achieve what I want properly.

Any help or hints on the proper method would be highly appreciated :)

  • 写回答

2条回答 默认 最新

  • duan0414 2017-10-11 20:57
    关注

    It is clearly not a good practice, as I stated in response to previous comments, I admit it, it is overkill in complexity for a simple API version management issue. I end up setting the parameter in parameters.yml file so when security.yml file is processed, it is not a problem, and placed my API management logic in the build process of the CI. Makes more sense. @Mocrates Thanks a lot for your answer :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试