我正在尝试在json-application
中实例化的简单服务中获取简单的原始symfony 4
请求,但它似乎与symfony 3
不同。
我的路线
deposit_money:
path: /auth/money/deposit
methods: [POST]
controller: App\Controller\MoneyController::deposit
defaults:
_format: json
我的控制器
public function deposit(MoneyService $moneyService)
{
$moneyService->depositMoney();
return new JsonResponse(
'Money sent bla bla', JsonResponse::HTTP_NO_CONTENT
);
}
我的服务:
class MoneyService
{
/** @var EntityManagerInterface */
private $entityManager;
/** @var RequestStack */
private $requestStack;
/**
* @param EntityManagerInterface $entityManager
* @param RequestStack $requestStack
*/
public function __construct(EntityManagerInterface $entityManager, RequestStack $requestStack)
{
$this->entityManager = $entityManager;
$this->requestStack = $requestStack;
}
public function depositMoney()
{
$this->requestStack->getCurrentRequest()->setRequestFormat('json');
var_dump($this->requestStack->getCurrentRequest()->getContent());
}
}
我的原始请求JSON(application/json)
{
"depositAmount":4
}
到目前为止没有问题,我发送请求并且完全收到但是...
在symfony 3
中,当我想获取请求参数时,就像这样
$this->requestStack->getCurrentRequest()->get('depositAmount')
而在symfony 4
中的这个新服务中,我首先需要以这种方式获取请求内容,以获得与PostMan一起发送的完全相同的原始json字符串。
$this->requestStack->getCurrentRequest()->getContent()
将遵循json转换等...
所以我的问题是...... 有没有办法像参与
那样简单地获取参数$this->requestStack->getCurrentRequest()->get('depositAmount')
我有什么遗漏或现在我需要将序列化注入服务并转换所有内容吗?