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
|
1 Answer
Reset to default 0I was able to get the value of email like this: $request->request->all()['update_email_form']['email']
toArray()
throws aJsonException
(although I don't see why it would cause a redirect). – Olivier Commented Feb 1 at 13:45Content-Type
is missing or incorrect. – Olivier Commented Feb 1 at 13:58toArray()
is for JSON only. – Olivier Commented Feb 1 at 14:13