**DefaultController.php**
这是我的DefaultController.php文件,我将在symfony中进入简单的登录页面 我想用控制器中提到的名字显示你好,请指导我,我是symfony的新人。
<?php
namespace AppBundle\Controller;
use AppBundle\Entity\users;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
class DefaultController extends Controller
{
/**
* @Route("/login", name="login")
*/
public function loginAction(Request $request)
{
if($request->getMethod()=='POST'){
$username=$request->get('username');
$password=$request->get('password');
$em=$this->getDoctrine()->getEntityManager();
$repository=$em->getRepository('AppBundle:users');
$user=$repository->findOneBy(array('userName'=>$username, 'password'=>$password));
if($user)
{
return $this->render('default/login.html.twig', array('name' =>$user->getFirstName()));
}
}
else{
return $this->render('default/login.html.twig');
}
}
}
**login.html.twig**
这是我在symfony中的登录页面,想要显示我在 Hello {{name}} 这一行中提到的名字你好,如果需要请做一些修改。
<html>
<head>
{% block stylesheets %}
<link rel="stylesheet" href="{{ asset('templates/css/1.css') }}">
{% endblock %}
</head>
<body>
{% block container %}
<div class="wrapper">
<h2>Login User</h2>
<form class="form-sigin" method="POST" action="{{path('login')}}">
<div><input type="text" name="user_name" class="txtbox" placeholder="User Name"></div>
<div><input type="password" name="password" class="txtbox" placeholder="Password"></div>
<div><input type="checkbox"><span>Remember</span></div>
<div><input type="submit" value="Log In" class="loginBtn"></div>
<div><span><a href="">Forgot Password</a></span></div>
</form>
</div>
{% if name is defined %}
<div class="alert-info fade in">
<strong>Hello {{name}}</strong>
</div>
{% endif %}
{% endblock %}
</body>
</html>
**users.php**
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* users
*
* @ORM\Table(name="users")
* @ORM\Entity(repositoryClass="AppBundle\Repository\usersRepository")
*/
class users
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="user_name", type="string", length=255)
*/
private $userName;
/**
* @var string
*
* @ORM\Column(name="first_name", type="string", length=255)
*/
private $firstName;
/**
* @var string
*
* @ORM\Column(name="password", type="string", length=255)
*/
private $password;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set userName
*
* @param string $userName
*
* @return users
*/
public function setUserName($userName)
{
$this->userName = $userName;
return $this;
}
/**
* Get userName
*
* @return string
*/
public function getUserName()
{
return $this->userName;
}
/**
* Set firstName
*
* @param string $firstName
*
* @return users
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* Get firstName
*
* @return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Set password
*
* @param string $password
*
* @return users
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
}
答案 0 :(得分:2)
你有一个糟糕的缩进来看问题..试试这个:
if($request->getMethod()=='POST')
{
$username=$request->get('username');
$password=$request->get('password');
$em=$this->getDoctrine()->getEntityManager();
$repository=$em->getRepository('AppBundle:users');
$user=$repository->findOneBy(array('userName'=>$username, 'password'=>$password));
if($user)
{
return $this->render('default/login.html.twig', array('name' =>$user->getFirstName()));
} else {
return $this->render('default/login.html.twig');
}
} else{
return $this->render('default/login.html.twig');
}
问题是你在POST请求中但是没有用户,所以它不会进入任何回复。
在这种情况下,我添加了一个新条件else
。
我不知道逻辑对你是否正确,但问题在于