Symfony2-无法自动加载JMS序列化程序注释


Symfony2 - unable to autoload JMS serializer annotations

我们在symfony应用程序中自动加载JMSSerializer注释时遇到问题。我们正在获得: [Semantical Error] The annotation "@JMS'Serializer'Annotation'XMLRoot" in class Class'Namespace'ClassName does not exist, or could not be auto-loaded.

我们使用标准的symfony/composer自动加载器,composer.json中需要"jms/serializer-bundle": "~1.0",并将捆绑包包含在AppKernel中。其他注释(例如symfony路由注释(工作正常。

我们试图通过将app_dev.php修改为:来强制加载jms序列化程序注释

<?php
use Doctrine'Common'Annotations'AnnotationRegistry;
use Symfony'Component'HttpFoundation'Request;
use Symfony'Component'Debug'Debug;
// If you don't want to setup permissions the proper way, just uncomment the following PHP line
// read http://symfony.com/doc/current/book/installation.html#configuration-and-setup for more information
//umask(0000);
$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
Debug::enable();
require_once __DIR__.'/../app/AppKernel.php';
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
AnnotationRegistry::registerFile("/srv/httpd/project/vendor/jms/serializer/src/JMS/Serializer/Annotation/XmlRoot.php");
AnnotationRegistry::registerAutoloadNamespace('JMS'Serializer', "/srv/httpd/project/vendor/jms/serializer/src/");
$kernel = new AppKernel('dev', true);
$kernel->loadClassCache();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

我们尝试了AnnotationRegistery::...调用的几种变体。AnnotationRegistry::registerFile("/srv/httpd/project/vendor/jms/serializer/src/JMS/Serializer/Annotation/XmlRoot.php");似乎正确注册了XmlRoot注释,但其他JMS注释仍然失败。

谢谢。

您必须编辑vendor/autoload.php文件,如下所示:

// autoload.php @generated by Composer
use Doctrine'Common'Annotations'AnnotationRegistry;
require_once __DIR__ . '/composer/autoload_real.php';
$loader = ComposerAutoloaderInitb6ddad78dfb081b4ad47d02feb034c25::getLoader();
AnnotationRegistry::registerLoader([$loader, 'loadClass']);
return $loader;

尝试使用别名将命名空间导入到您的Entity

<?php
use JMS'Serializer'Annotation as JMS
class SomeClass
{
    /**
     * @JMS/XMLRoot
     */
    public $someProperty
}

Doctrine在其他地方也有同样的问题。

同样错误的另一个可能原因:-

[Semantical Error] The annotation "@JMS'Serializer'Annotation'XMLRoot" in
class Class'Namespace'ClassName does not exist, or could not be auto-loaded.

是对AnnotationRegistry::registerLoader的丢失调用。

请参阅此处的文档-http://jmsyst.com/libs/serializer/master/configuration

如果您正在使用独立库,并且希望使用注释,则必须初始化注释注册表:

条令''Common''Annotations''AnnotationRegister::registerLoader('class_exists'(