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条)

报告相同问题?

悬赏问题

  • ¥18 深度学习tensorflow1,ssdv1,coco数据集训练一个模型
  • ¥100 关于注册表摄像头和麦克风的问题
  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题
  • ¥15 主机可以ping通路由器但是连不上网怎么办
  • ¥15 数据库一张以时间排好序的表中,找出多次相邻的那些行
  • ¥50 关于DynamoRIO处理多线程程序时候的问题
  • ¥15 kubeadm部署k8s出错
  • ¥15 Abaqus打不开cae文件怎么办?
  • ¥15 小程序准备上线,软件开发公司需要提供哪些资料给甲方