簡體   English   中英

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" 〜1.0 "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 \\注解\\ AnnotationRegistry :: registerLoader( 'class_exists');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM