如何在 API-Platform 上使用“paramconverter?
How to use "paramconverter with API-Platform?
如何在 Symfony API 平台上实现或使用参数转换器?
我想在路由上使用实体 ID 并立即生成一个对象,准备在控制器中使用。
我没有在这个项目上使用注释。路由配置在 YAML 文件中。
resources:
App\Meetings\Domain\Entity\Meeting:
collectionOperations:
invitation_response:
method: 'POST'
path: 'users/{id}'
controller: 'App\Controller\User\IndexController'
defaults:
_api_receive: false
为什么不使用 decorator?
例如如果您的控制器是这样的:
class IndexController {
public function __invoke(CustomClass $object) {
// do your thing
// return a Response
}
你可以构建一个装饰器CustomClassConverterController
class CustomClassConverter {
protected $innerController;
protected $em;
public function (IndexController $controller, EntityManagerInterface $em) {
$this->innerController = $controller;
$this->em = $em;
}
public function __invoke($id) {
$object = $this->em->getRepository(CustomClass::class)->findOne((int) $id);
if (! $object instanceof CustomClass) {
throw new NotFoundHttpException('Custom class ' . $id . ' not found');
}
return $this->innerController($object);
}
}
您需要添加此配置才能激活装饰:
services:
App\Controller\IndexController: ~
App\Decorator\CustomClassConverterController:
decorates: App\Controller\IndexController
如何在 Symfony API 平台上实现或使用参数转换器?
我想在路由上使用实体 ID 并立即生成一个对象,准备在控制器中使用。
我没有在这个项目上使用注释。路由配置在 YAML 文件中。
resources:
App\Meetings\Domain\Entity\Meeting:
collectionOperations:
invitation_response:
method: 'POST'
path: 'users/{id}'
controller: 'App\Controller\User\IndexController'
defaults:
_api_receive: false
为什么不使用 decorator?
例如如果您的控制器是这样的:
class IndexController {
public function __invoke(CustomClass $object) {
// do your thing
// return a Response
}
你可以构建一个装饰器CustomClassConverterController
class CustomClassConverter {
protected $innerController;
protected $em;
public function (IndexController $controller, EntityManagerInterface $em) {
$this->innerController = $controller;
$this->em = $em;
}
public function __invoke($id) {
$object = $this->em->getRepository(CustomClass::class)->findOne((int) $id);
if (! $object instanceof CustomClass) {
throw new NotFoundHttpException('Custom class ' . $id . ' not found');
}
return $this->innerController($object);
}
}
您需要添加此配置才能激活装饰:
services:
App\Controller\IndexController: ~
App\Decorator\CustomClassConverterController:
decorates: App\Controller\IndexController