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 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错