最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Symfony 7 - $request->toArray() causes redirection - Stack Overflow

programmeradmin1浏览0评论

I have the following method in a controller in Symfony 7:

#[Route('/settings/email', name: 'settings.email', methods: ['PUT'])]
public function updateEmail(Request $request): Response
{
    $data = $request->toArray();
    dd($data);
}

I would like to get the email value from the body, however, when I use $request->toArray() it causes a redirect to the current page (for example: I am at /settings and it redirects to /settings, the dd isn't triggered).

How to solve that?

Edit:

The method is called from a form:

<form name="update_email_form" method="post" action="/settings/email">
   <input type="hidden" name="_method" value="PUT">
   <div id="update_email_form">
      <div class="mb-3"><label for="update_email_form_email" class="form-label required">Email</label><input type="email" id="update_email_form_email" name="update_email_form[email]" required="required" maxlength="255" class="form-control"></div>
      <div class="mb-3"><button type="submit" id="update_email_form_update" name="update_email_form[update]" class="btn-primary btn">Update</button></div>
      <input type="hidden" id="update_email_form__token" name="update_email_form[_token]" data-controller="csrf-protection" autocomplete="off" value="csrf-token">
   </div>
</form>

I have the following method in a controller in Symfony 7:

#[Route('/settings/email', name: 'settings.email', methods: ['PUT'])]
public function updateEmail(Request $request): Response
{
    $data = $request->toArray();
    dd($data);
}

I would like to get the email value from the body, however, when I use $request->toArray() it causes a redirect to the current page (for example: I am at /settings and it redirects to /settings, the dd isn't triggered).

How to solve that?

Edit:

The method is called from a form:

<form name="update_email_form" method="post" action="/settings/email">
   <input type="hidden" name="_method" value="PUT">
   <div id="update_email_form">
      <div class="mb-3"><label for="update_email_form_email" class="form-label required">Email</label><input type="email" id="update_email_form_email" name="update_email_form[email]" required="required" maxlength="255" class="form-control"></div>
      <div class="mb-3"><button type="submit" id="update_email_form_update" name="update_email_form[update]" class="btn-primary btn">Update</button></div>
      <input type="hidden" id="update_email_form__token" name="update_email_form[_token]" data-controller="csrf-protection" autocomplete="off" value="csrf-token">
   </div>
</form>
Share Improve this question edited Feb 1 at 14:03 Raphael asked Feb 1 at 13:25 RaphaelRaphael 7291 gold badge12 silver badges28 bronze badges 5
  • 3 Maybe toArray() throws a JsonException (although I don't see why it would cause a redirect). – Olivier Commented Feb 1 at 13:45
  • @Olivier Yes, it throws an exception: Could not decode request body. – Raphael Commented Feb 1 at 13:57
  • Then either the JSON is invalid or the Content-Type is missing or incorrect. – Olivier Commented Feb 1 at 13:58
  • @Olivier I call the method from a form (I updated the question). How do I know if the JSON is invalid or the Content-Type is missing or incorrect? – Raphael Commented Feb 1 at 14:04
  • 3 You are not sending a JSON here but a form. toArray() is for JSON only. – Olivier Commented Feb 1 at 14:13
Add a comment  | 

1 Answer 1

Reset to default 0

I was able to get the value of email like this: $request->request->all()['update_email_form']['email']

发布评论

评论列表(0)

  1. 暂无评论