api-platform.com无法为类型的项目生成IRI [英] api-platform.com Unable to generate an IRI for the item of type

查看:97
本文介绍了api-platform.com无法为类型的项目生成IRI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置我的第一个api平台实例,并遇到了一些有关IDE的新手问题.我想做的是设置一个注册和重置实体,以便处理来自应用程序的注册和密码提醒.然后将实体传递给自定义控制器,然后再执行一些检查,余额和邮件处理(还没有解决).但是在发布到自定义实体时,我总是遇到此错误:

I'm trying to setup my first api-platform instance and have run into some presumably noob questions around IDE. What I'm trying to do is setting up an register and reset entity in order to deal with registrations and password reminders from an app. And then have the entity's passed to a custom controller to do some checks, balances and mails afterwards (haven’t gotten around to that yet). But I keep running into this error when posting to my custom entities:

{
    "@context": "/api/contexts/Error",
    "@type": "hydra:Error",
    "hydra:title": "An error occurred",
    "hydra:description": "Unable to generate an IRI for the item of type \"App\\XXX\\UserBundle\\Entity\\Register\"",
    "trace": [
        {
            "namespace": "",
            "short_class": "",
            "class": "",
            "type": "",
            "function": "",
            "file": "/var/www/webservice/vendor/api-platform/core/src/Bridge/Symfony/Routing/IriConverter.php",
            "line": 116,
            "args": []
        },
        {
            "namespace": "ApiPlatform\\Core\\Bridge\\Symfony\\Routing",
            "short_class": "IriConverter",
            "class": "ApiPlatform\\Core\\Bridge\\Symfony\\Routing\\IriConverter",
            "type": "->",
            "function": "getIriFromItem",
            "file": "/var/www/webservice/vendor/api-platform/core/src/JsonLd/Serializer/ItemNormalizer.php",
            "line": 71,
            "args": [
                [
                    "object",
                    "App\\XXX\\UserBundle\\Entity\\Register"
                ]
            ]
        },
        {
            "namespace": "ApiPlatform\\Core\\JsonLd\\Serializer",
            "short_class": "ItemNormalizer",
            "class": "ApiPlatform\\Core\\JsonLd\\Serializer\\ItemNormalizer",
            "type": "->",
            "function": "normalize",
            "file": "/var/www/webservice/vendor/symfony/serializer/Serializer.php",
            "line": 138,
            "args": [
                [
                    "object",
                    "App\\XXX\\UserBundle\\Entity\\Register"
                ],
                [
                    "string",
                    "jsonld"
                ],
                [
                    "array",
                    {
                        "collection_operation_name": [
                            "string",
                            "post"
                        ],
                        "operation_type": [
                            "string",
                            "collection"
                        ],
                        "resource_class": [
                            "string",
                            "App\\XXX\\UserBundle\\Entity\\Register"
                        ],
                        "request_uri": [
                            "string",
                            "/api/register"
                        ],
                        "uri": [
                            "string",
                            "http://webservice.local/api/register"
                        ],
                        "resources": [
                            "object",
                            "ApiPlatform\\Core\\Serializer\\ResourceList"
                        ],
                        "jsonld_has_context": [
                            "boolean",
                            true
                        ]
                    }
                ]
            ]
        },
        {
            "namespace": "Symfony\\Component\\Serializer",
            "short_class": "Serializer",
            "class": "Symfony\\Component\\Serializer\\Serializer",
            "type": "->",
            "function": "normalize",
            "file": "/var/www/webservice/vendor/symfony/serializer/Serializer.php",
            "line": 111,
            "args": [
                [
                    "object",
                    "App\\XXX\\UserBundle\\Entity\\Register"
                ],
                [
                    "string",
                    "jsonld"
                ],
                [
                    "array",
                    {
                        "collection_operation_name": [
                            "string",
                            "post"
                        ],
                        "operation_type": [
                            "string",
                            "collection"
                        ],
                        "resource_class": [
                            "string",
                            "App\\XXX\\UserBundle\\Entity\\Register"
                        ],
                        "request_uri": [
                            "string",
                            "/api/register"
                        ],
                        "uri": [
                            "string",
                            "http://webservice.local/api/register"
                        ],
                        "resources": [
                            "object",
                            "ApiPlatform\\Core\\Serializer\\ResourceList"
                        ]
                    }
                ]
            ]
        },
        {
            "namespace": "Symfony\\Component\\Serializer",
            "short_class": "Serializer",
            "class": "Symfony\\Component\\Serializer\\Serializer",
            "type": "->",
            "function": "serialize",
            "file": "/var/www/webservice/vendor/api-platform/core/src/EventListener/SerializeListener.php",
            "line": 67,
            "args": [
                [
                    "object",
                    "App\\XXX\\UserBundle\\Entity\\Register"
                ],
                [
                    "string",
                    "jsonld"
                ],
                [
                    "array",
                    {
                        "collection_operation_name": [
                            "string",
                            "post"
                        ],
                        "operation_type": [
                            "string",
                            "collection"
                        ],
                        "resource_class": [
                            "string",
                            "App\\XXX\\UserBundle\\Entity\\Register"
                        ],
                        "request_uri": [
                            "string",
                            "/api/register"
                        ],
                        "uri": [
                            "string",
                            "http://webservice.local/api/register"
                        ],
                        "resources": [
                            "object",
                            "ApiPlatform\\Core\\Serializer\\ResourceList"
                        ]
                    }
                ]
            ]
        },
        {
            "namespace": "ApiPlatform\\Core\\EventListener",
            "short_class": "SerializeListener",
            "class": "ApiPlatform\\Core\\EventListener\\SerializeListener",
            "type": "->",
            "function": "onKernelView",
            "file": "/var/www/webservice/vendor/symfony/event-dispatcher/EventDispatcher.php",
            "line": 212,
            "args": [
                [
                    "object",
                    "Symfony\\Component\\HttpKernel\\Event\\GetResponseForControllerResultEvent"
                ],
                [
                    "string",
                    "kernel.view"
                ],
                [
                    "object",
                    "Symfony\\Component\\EventDispatcher\\EventDispatcher"
                ]
            ]
        },
        {
            "namespace": "Symfony\\Component\\EventDispatcher",
            "short_class": "EventDispatcher",
            "class": "Symfony\\Component\\EventDispatcher\\EventDispatcher",
            "type": "->",
            "function": "doDispatch",
            "file": "/var/www/webservice/vendor/symfony/event-dispatcher/EventDispatcher.php",
            "line": 44,
            "args": [
                [
                    "array",
                    [
                        [
                            "array",
                            [
                                [
                                    "object",
                                    "ApiPlatform\\Core\\Validator\\EventListener\\ValidateListener"
                                ],
                                [
                                    "string",
                                    "onKernelView"
                                ]
                            ]
                        ],
                        [
                            "array",
                            [
                                [
                                    "object",
                                    "ApiPlatform\\Core\\EventListener\\WriteListener"
                                ],
                                [
                                    "string",
                                    "onKernelView"
                                ]
                            ]
                        ],
                        [
                            "array",
                            [
                                [
                                    "object",
                                    "ApiPlatform\\Core\\EventListener\\SerializeListener"
                                ],
                                [
                                    "string",
                                    "onKernelView"
                                ]
                            ]
                        ],
                        [
                            "array",
                            [
                                [
                                    "object",
                                    "ApiPlatform\\Core\\EventListener\\RespondListener"
                                ],
                                [
                                    "string",
                                    "onKernelView"
                                ]
                            ]
                        ]
                    ]
                ],
                [
                    "string",
                    "kernel.view"
                ],
                [
                    "object",
                    "Symfony\\Component\\HttpKernel\\Event\\GetResponseForControllerResultEvent"
                ]
            ]
        },
        {
            "namespace": "Symfony\\Component\\EventDispatcher",
            "short_class": "EventDispatcher",
            "class": "Symfony\\Component\\EventDispatcher\\EventDispatcher",
            "type": "->",
            "function": "dispatch",
            "file": "/var/www/webservice/vendor/symfony/http-kernel/HttpKernel.php",
            "line": 154,
            "args": [
                [
                    "string",
                    "kernel.view"
                ],
                [
                    "object",
                    "Symfony\\Component\\HttpKernel\\Event\\GetResponseForControllerResultEvent"
                ]
            ]
        },
        {
            "namespace": "Symfony\\Component\\HttpKernel",
            "short_class": "HttpKernel",
            "class": "Symfony\\Component\\HttpKernel\\HttpKernel",
            "type": "->",
            "function": "handleRaw",
            "file": "/var/www/webservice/vendor/symfony/http-kernel/HttpKernel.php",
            "line": 66,
            "args": [
                [
                    "object",
                    "Symfony\\Component\\HttpFoundation\\Request"
                ],
                [
                    "integer",
                    1
                ]
            ]
        },
        {
            "namespace": "Symfony\\Component\\HttpKernel",
            "short_class": "HttpKernel",
            "class": "Symfony\\Component\\HttpKernel\\HttpKernel",
            "type": "->",
            "function": "handle",
            "file": "/var/www/webservice/vendor/symfony/http-kernel/Kernel.php",
            "line": 188,
            "args": [
                [
                    "object",
                    "Symfony\\Component\\HttpFoundation\\Request"
                ],
                [
                    "integer",
                    1
                ],
                [
                    "boolean",
                    true
                ]
            ]
        },
        {
            "namespace": "Symfony\\Component\\HttpKernel",
            "short_class": "Kernel",
            "class": "Symfony\\Component\\HttpKernel\\Kernel",
            "type": "->",
            "function": "handle",
            "file": "/var/www/webservice/public/index.php",
            "line": 37,
            "args": [
                [
                    "object",
                    "Symfony\\Component\\HttpFoundation\\Request"
                ]
            ]
        }
    ]
}

我当然自己做过一些研究,并且对两者进行了研究:

I did of course do some research on my own and had a look at both:

  • Unable to generate an IRI for the item of type
  • api platform - Unable to generate an IRI for the item
  • Request validator : validate an IRI
  • https://github.com/api-platform/core/issues/830

因此,我添加了一个ID(因为此实体仅用于发布的ID,因此不需要此ID),清除了现金,检查了吸气剂和塞脂剂,但都无济于事.

So I've added an id (which is not needed since this entity is just used for post's), cleared the cash, checked the getters and setters but all to no avail.

该实体当前看起来像这样

The entity currently looks like this

/**
 * The register Entity
 *
 * @ApiResource(
 *      collectionOperations={
 *          "post"={"method"="POST", "path"="/register", "controller" = RegisterController::class}          
 *      }
 * )
 */

class Register implements \Serializable
{
    /**
     * The unique identifier of this user <br /><b>Schema:</b> <a href="http://schema.org/identifier">https://schema.org/identifier</a>
     *
     * @var integer
     *
     * @ApiProperty(iri="https://schema.org/identifier", identifier=true)
     */
    protected $id;  

    /**
     * The unique username of this user, used as a security credential alongside the password
     * 
     * @var string
     *
     */
    protected $username;

    /**
     * The non-unique e-mail address, used to contact the user on security issues
     * 
     * @var string
     *
     * @ApiProperty(iri="http://schema.org/email")
     */
    protected $email;

    /**
     * The non-unique password of this user, used as a security credential alongside the e-mail. Passwords are hashed in the database and can therefore never be rendered or viewed
     * 
     * @var string
     *
     */
    protected $password;

    /**
     * Serializing the user data that is set into the session
     */
    /** @see \Serializable::serialize() */
    public function serialize()
    {
        return serialize(array(
                $this->id,
                $this->username,
                $this->password,
                $this->email
        ));
    }

    /** @see \Serializable::unserialize() */
    public function unserialize($serialized)
    {
        list (
                $this->id,
                $this->username,
                $this->password,
                $this->email,
                ) = unserialize($serialized);
    }


    /**
     * Get id.
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Get id.
     *
     * @return User
     */
    public function setId($id)
    {
        $this->id= $id;

        return $this;
    }   

    /**
     * Set username
     *
     * @param string $username
     *
     * @return User
     */
    public function setUsername($username)
    {
        $this->username = $username;

        return $this;
    }

    public function getUsername()
    {
        return $this->username;
    }

    /**
     * Set password
     *
     * @param string $password
     *
     * @return User
     */
    public function setPassword($password)
    {
        $this->password = $password;

        return $this;
    }

    public function getPassword()
    {
        return $this->password;
    }


    /**
     * Set email.
     *
     * @param string $email
     *
     * @return User
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Get email.
     *
     * @return string
     */
    public function getEmail()
    {
        return $this->email;
    }
}

控制器(尚未完全完成,但首先尝试解决):

The controller (not quite finished but trying to solve this first):

/**
 * Register controller
 *
 * @category   Controller
 * @package    XXX\UserBundle
 * @subpackage Controller
 *
 */

namespace App\XXX\UserBundle\Controller;


use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use App\XXX\UserBundle\Service\UserService;
use App\XXX\UserBundle\Entity\User;
use App\XXX\UserBundle\Entity\Register;

class RegisterController extends Controller
{
    private $userService;

    public function __construct(UserService $userService)
    {
        $this->userService= $userService;
    }

    public function __invoke(Register $data): Register
    {
        //$this->myService->doSomething($data);

        /*
        $user = $this->userService->register($data);

        if($user){
            //  $token = JWTManager::create($user)

            // voorbeeld

            // return serialized token
            // @var JWTManager $jwtManager 
            $jwtManager = $this->get('lexik_jwt_authentication.jwt_manager');
            $jwt = $jwtManager->create($user);
            $response = new JsonResponse();
            $response->setData(array('token' => $jwt));
            return $response;
        }
         */
        return $data;
    }


}

最后但并非最不重要的config/routes/api_platform.yaml

and last but not least config/routes/api_platform.yaml

api_platform:
    resource: .
    type: api_platform
    prefix: /api


user_register:
    path: '/api/register'
    methods:  ['POST']
    defaults:
        _controller: '\App\XXX\UserBundle\RegisterController::class'
        _api_resource_class: 'App\XXX\UserBundle\Entity\Register'
        _api_item_operation_name: 'register'

user_reminder:
    path: '/api/reset'
    methods:  ['POST']
    defaults:
        _controller: '\App\XXX\UserBundle\ResetController::class'
        _api_resource_class: 'App\XXX\UserBundle\Entity\Reset'
        _api_item_operation_name: 'rest'

user_login:
    path: '/api/login'
    methods:  ['POST']
    defaults:
        _controller: '\App\XXX\UserBundle\LoginController::class'
        _api_resource_class: 'App\XXX\UserBundle\Entity\Login'
        _api_item_operation_name: 'login'

我可能在这里忽略了一些基本知识,但是我花了数小时,似乎无法正常工作.

I'm probably overlooking something basic here, but I've spent hours on it and just can't seem to get it to work.

-更新- 我将所有内容都恢复为基本状态(用于测试和错误查找),但它不会运行.添加了get/set,甚至将ID公开,它将无法正常工作.即使是下面这个简单的类,也让我遇到了IRI错误.因此,基本上我尝试过的所有不是ORM对象的操作都会失败.

--- Update --- I changed everything back to basic (for testing and error hunting) and it just wont fly. Added get/set and even made ID public, it just wont work. Even this simple class below gets me the IRI error. So basically everything I try that isn't an ORM object fails.

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;

/**
 * The user Entity
 *
 * @ApiResource(
 *      itemOperations={
 *          "get"={"method"="GET"},
 *      },
 *      collectionOperations={
 *          "get"={"method"="GET"},
 *          "register"={"route_name"="register_register"},
 *      },
 * )
 */

class Register 
{
    /**
     * The unique identifier of this user <br /><b>Schema:</b> <a href="http://schema.org/identifier">https://schema.org/identifier</a>
     * 
     * @var integer
     *
     * @ApiProperty(identifier=true)
     */
    public  $id;    


    /**
     * Get id.
     *
     * @return int
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }


    /**
     * Get id.
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }
}

推荐答案

我是api平台的新手,并且遇到相同的问题.以下解决方案为我工作.在 collectionOperations 注释部分的post方法中添加 normalization_context .

I am new to api platform and had the same issue. The following solution worked for me. Add a normalization_context to the post method in collectionOperations annotation section.

使用您的第一个Register类,您将拥有:

Using your first Register class you would have:

/**
     * The register Entity
     *
     * @ApiResource(
     *      collectionOperations={
     *          "post"=
     *           {
     *              "method"="POST",
     *              "path"="/register",
     *              "controller" = RegisterController::class,
     *              "normalization_context"={"groups"={"common:read"}}
     *           }          
     *      }
     * )
     */

    class Register implements \Serializable
    {

然后将相同的组放在要返回的字段/属性上方.继续上面的Register类:在每个属性上方添加的值是 @Groups("common:read"):

Then put the same group above the fields/properties you wish to return. Continuing with the Register class above: the value added above each property is @Groups("common:read") :

/**
     * The unique identifier of this user <br /><b>Schema:</b> <a href="http://schema.org/identifier">https://schema.org/identifier</a>
     *
     * @var integer
     *
     * @Groups("common:read")
     *
     * @ApiProperty(iri="https://schema.org/identifier", identifier=true)
     */
    protected $id;  

    /**
     * The unique username of this user, used as a security credential alongside the password
     * 
     * @var string
     *
     * @Groups("common:read")
     *
     */
    protected $username;

    /**
     * The non-unique e-mail address, used to contact the user on security issues
     * 
     * @var string
     *
     * @Groups("common:read")
     *
     * @ApiProperty(iri="http://schema.org/email")
     */
    protected $email;

这篇关于api-platform.com无法为类型的项目生成IRI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆