這是我的登錄模板
{% 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 %}
我想將資訊發送給兩個控制器
控制器 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]);
}
控制器 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,]);
}
我應該怎么做才能將資訊發送給兩個控制器?在 1 號控制器中,我需要以某種方式登錄用戶名,我突然想到用 $_POST['_username'] 檢索它
uj5u.com熱心網友回復:
我認為您要做的是讓用戶登錄,然后向他們展示他們的引文。這個想法不是發送兩個表單,而是在驗證登錄后重定向。
#[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);
}
您可以根據需要通過引數(我的示例)或查詢傳遞用戶名。然后,修改您的 Controller 1 以通過引數獲取用戶名。
#[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]);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/503885.html
標籤:php html 交响乐 枝条 symfony5.4
上一篇:一組復選框,類似于單選按鈕
