【问题标题】:Symfony2 AnnotationException You have to enable OPCache or ZendOptimizerSymfony2 AnnotationException 你必须启用 OPCache 或 ZendOptimizer
【发布时间】:2015-08-07 04:15:23
【问题描述】:

我有基于 Symfony2 框架的应用程序。我已经转移到生产环境,网站运行良好(我正在尝试调整缓存 - 在我的主机上是 xcache)。但是突然我有这个错误:

致命错误:未捕获的异常 'Doctrine\Common\Annotations\AnnotationException' 带有消息'你 必须启用 opcache.save_cmets=1 或 zend_optimizerplus.save_cmets=1。在 /nfsmnt/hosting2_1/6/9/699be0da-dfbd-4651-90de-448d295bb741/playsport.sk/web/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationException.php:183 堆栈跟踪:#0 /nfsmnt/hosting2_1/6/9/699be0da-dfbd-4651-90de-448d295bb741/playsport.sk/web/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationReader.php(162): Doctrine\Common\Annotations\AnnotationException::optimizerPlusSaveComments()

1 /nfsmnt/hosting2_1/6/9/699be0da-dfbd-4651-90de-448d295bb741/playsport.sk/web/app/cache/prod/appProdProjectContainer.php(237):

Doctrine\Common\Annotations\AnnotationReader->__construct() #2 /nfsmnt/hosting2_1/6/9/699be0da-dfbd-4651-90de-448d295bb741/playsport.sk/web/app/bootstrap.php.cache(2103): appProdProjectContainer->getAnnotationReaderService() #3 /nfsmnt/hosting2_1/6/9/699be0da-dfbd-4651-90de-448d295bb741/playsport.sk/web 在 /nfsmnt/hosting2_1/6/9/699be0da-dfbd-4651-90de-448d295bb741/playsport.sk/web/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationException.php 在第 183 行

我无法在我的主机上启用 opcache.save_cmets(我没有特权,这是临时付费托管服务)。因此,我禁用了 prod 环境中的所有缓存并尝试重新运行网站。我目前的配置:

web/app.php:

 $loader = require_once __DIR__.'/../app/bootstrap.php.cache';
    
    // Enable APC for autoloading to improve performance.
    // You should change the ApcClassLoader first argument to a unique prefix
    // in order to prevent cache key conflicts with other applications
    // also using APC.
    
    //$apcLoader = new ApcClassLoader(sha1(__FILE__), $loader);
    //$xcacheLoader = new \Symfony\Component\ClassLoader\XcacheClassLoader(sha1(__FILE__) . "PlaySport", $loader);
    //$loader->unregister();
    //$xcacheLoader->register(true);
    
    
    require_once __DIR__.'/../app/AppKernel.php';
    // require_once __DIR__.'/../app/AppCache.php';
    
    $kernel = new AppKernel('prod', false);
    $kernel->loadClassCache();
    //$kernel = new AppCache($kernel);
    
    // When using the HttpCache, you need to call the method in your front controller instead of relying on the configuration parameter
    //Request::enableHttpMethodParameterOverride();
    $request = Request::createFromGlobals();
    $response = $kernel->handle($request);
    $response->send();
    $kernel->terminate($request, $response);

config_prod.yml:

    imports:
        - { resource: config.yml }
    
    #framework:
    #    validation:
    #        cache: xcache
    
    #doctrine:
    #    orm:
    #        metadata_cache_driver: xcache
    #        result_cache_driver: xcache
    #        query_cache_driver: xcache
    
    monolog:
        handlers:
            main:
                type:         fingers_crossed
                action_level: error
                handler:      nested
            nested:
                type:  stream
                path:  "%kernel.logs_dir%/%kernel.environment%.log"
                level: debug
            console:
                type:  console

我真的不知道该怎么办。仍然像以前一样出错。如何禁用 Doctrine 中的注释缓存或该问题的任何其他解决方案。谢谢!

【问题讨论】:

    标签: symfony doctrine-orm annotations doctrine opcache


    【解决方案1】:

    问题解决了!这是由虚拟主机提供商和服务器配置引起的。 Opcache 或 Zend Optimizer 配置不正确

    【讨论】:

    • 真正的原因是什么?哪个配置解决了这个问题?
    • 您必须将 zend_optimizerplus.save_cmets 或 opcache.save_cmets 设置为 true。
    • 您可以在 php-fpm/conf.d 中删除指向 opcache 的符号链接(如果您不需要),例如 /etc/php/7.0/fpm/conf.d - 删除 @10-opcache.ini ...
    • 这不是答案!请编辑您的答案以记录确切的步骤或取消选择当前答案。任何到达这里的人都不会觉得这个答案有用。
    【解决方案2】:

    您能否在您的主机上禁用 Zend OpCache 和/或 Zend Optimizer+ 扩展?似乎如果禁用它,您将摆脱这个错误,因为 Doctrine 不会对 opcache.save_cmets 参数进行额外检查。

    \Doctrine\Common\Annotations\AnnotationReader.php 中:

    if (extension_loaded('Zend Optimizer+') && (ini_get('zend_optimizerplus.save_comments') === "0" || ini_get('opcache.save_comments') === "0")) {
      throw AnnotationException::optimizerPlusSaveComments();
    }
    
    if (extension_loaded('Zend OPcache') && ini_get('opcache.save_comments') == 0) {
        throw AnnotationException::optimizerPlusSaveComments();
    }
    

    禁用任何缓存都不会修复此错误,因为此检查是在 AnnotationReader 类的构造函数中完成的,它不依赖于任何缓存。

    如果不能,您仍然可以尝试使用 ini_set PHP 命令将 opcache.save_cmets 参数设置为 1。

    【讨论】:

    • 我正在使用 websupport.sk 托管服务(斯洛伐克最好的提供商)。由于权限,我无法通过 .htaccess 更改 opcache.save_cmets (我在提问之前尝试过)。我试图向提供商寻求解决方案,现在我正在等待回复。但有趣的是,那个网页运行正常,突然出现错误。
    • 也许他们最近更改了配置。您是否尝试过使用 ini_set() 的技巧?如果你不能用 .htaccess 文件改变它,我不确定这是否会改变任何东西,但你还是应该试试。
    猜你喜欢
    • 2011-10-06
    • 2014-09-06
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    • 2017-01-06
    • 1970-01-01
    • 2015-08-02
    相关资源
    最近更新 更多