Can I send the same information from a form to 2 action in symfony5?

Issue

This Content is from Stack Overflow. Question asked by Miguel Ángel

THIS IS MY LOGIN TEMPLATE

{% extends 'base.html.twig' %}

{% block title %}Hello LoginController!{% endblock %}

{% block body %}
    {% if error %}
        <div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
    {% endif %}

    <form action="{{ path('app_login') }}" method="post">
        <label for="username">Username:</label>
        <input type="text" id="username" name="_username" value="{{ last_username }}"/>

        <label for="password">Password:</label>
        <input type="password" id="password" name="_password"/>

        <button type="submit">login</button>
    </form>
{% endblock %}

AND I WANT SEND THE INFORMATION TO BOTH CONTROLLER

CONTROLLER 1

#[Route('/consulta', name:'cita_consulta')]
    public function consultas(ManagerRegistry $doctrine)
    {
        $username=$_POST['_username'];
        $citaRepository = new CitaRepository($doctrine);
        $citas = $citaRepository->findAll();

        return $this->render('familia/reservas.html.twig', ['citas' => $citas,'username' => $username]);
    }

CONTROLLER 2

 #[Route('/login', name: 'app_login')]
    public function index(AuthenticationUtils $authenticationUtils): Response
        {
                // get the login error if there is one
                $error = $authenticationUtils->getLastAuthenticationError();
                // last username entered by the user
                $lastUsername = $authenticationUtils->getLastUsername();
                
                return $this->render('login/index.html.twig', ['last_username' => $lastUsername,'error'=> $error,]);
        }

What should I do to send the information to both controllers? In controller number 1 I need the username to be logged in somehow and it occurred to me to retrieve it with a $_POST[‘_username’]



Solution

Check the Answers

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?