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

时间:2015-09-29 16:41:42

标签: php symfony annotations jms-serializer

我们在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中包含该bundle。其他注释(例如symfony route annotations)正常工作。

我们尝试通过修改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注释仍然失败。

感谢。

4 个答案:

答案 0 :(得分:2)

您必须编辑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;

答案 1 :(得分:0)

尝试使用别名

将命名空间导入到Entity
<?php

use JMS\Serializer\Annotation as JMS

class SomeClass
{
    /**
     * @JMS/XMLRoot
     */
    public $someProperty
}

在其他地方Doctrine遇到同样的问题。

答案 2 :(得分:0)

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

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

缺少对AnnotationRegistry::registerLoader的调用。

答案 3 :(得分:0)

在此处查看文档-http://jmsyst.com/libs/serializer/master/configuration

  

如果您使用的是独立库,并且要使用批注,则必须初始化批注注册表:

     

Doctrine \ Common \ Annotations \ AnnotationRegistry :: registerLoader('class_exists');