1

我们在 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 自动加载器,"jms/serializer-bundle": "~1.0"在 composer.json 中需要,并将包包含在 AppKernel 中。其他注释(例如 symfony 路由注释)可以正常工作。

我们尝试通过将 app_dev.php 修改为:

<?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

4 回答 4

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;
于 2018-05-07T10:14:50.570 回答
0

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

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

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

于 2019-03-19T09:48:58.617 回答
0

Entity尝试使用别名将命名空间导入 Your :

<?php

use JMS\Serializer\Annotation as JMS

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

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

于 2015-09-29T19:29:16.530 回答
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.

于 2018-01-18T16:01:33.637 回答