phphtmlsymfonytwigsymfony5

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


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

  • I think what you're trying to do is get the user to login and then show them their citations. The idea is not to send two forms, but to redirect once the login has been verified.

    #[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();
            
            if ($error) {
                return $this->render('login/index.html.twig', ['last_username' => $lastUsername,'error'=> $error,]);
            }
    
            return $this->redirectToRoute('cita_consulta', ['username' => $lastUsername], 301);
    }
    

    You can pass the username by params (my example) or query as you want. Then, modify your Controller 1 to get the username by params.

    #[Route('/consulta/{username}', name:'cita_consulta')]
    public function consultas(string $username, ManagerRegistry $doctrine)
    {
        $citaRepository = new CitaRepository($doctrine);
        $citas = $citaRepository->findAll();
    
        return $this->render('familia/reservas.html.twig', ['citas' => $citas,'username' => $username]);
    }