我正在尝试设置我的第一个api平台实例,并且在IDE周围遇到了一些可能是noob的问题。我试图做的是建立一个注册和重置实体,以处理注册和密码提醒从一个应用程序。然后,将实体传递给自定义控制器,以便在事后进行一些检查、平衡和邮件(目前还没有做到这一点)。但是,当我向我的自定义实体发布时,我经常会遇到这个错误:
{
"@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"
]
]
}
]
}当然,我自己做了一些研究,并对这两个方面进行了研究:
因此,我添加了一个id (因为这个实体只用于post),清除了现金,检查了getter和setter,但是都没有效果。
该实体当前的外观如下
/**
* 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;
}
}控制器(尚未完全完成,但试图先解决这个问题):
/**
* 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;
}
}最后但并非最不重要的配置/路由/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'我可能忽略了一些基本的东西,但我已经花了好几个小时在上面,似乎无法让它起作用。
--更新--我把所有的东西都改回了basic (用于测试和错误搜索),它就不会飞了。增加了get/set,甚至公开了ID,这是行不通的。甚至下面这个简单的类也给我带来了IRI错误。所以基本上,我所尝试的不是ORM对象的所有东西都失败了。
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;
}
}发布于 2018-10-31 13:41:38
你看过这个线程api platform - Unable to generate an IRI for the item吗?
通常,当无法访问属性Id时,会发生此错误。
发布于 2019-05-31 13:30:38
我是api平台的新手,也有过同样的问题。下面的解决方案对我有效。将normalization_context添加到collectionOperations注释部分中的post方法中。
使用您的第一个注册类,您将拥有:
/**
* 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"):
/**
* 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;https://stackoverflow.com/questions/51136184
复制相似问题