**DefaultController.php**
这是我的DefaultController.php文件,我要去symfony中的简单登录页面我想用我在控制器中提到的名字显示你好,请引导我我是symfony的新手。
<?php
namespace AppBundleController;
use AppBundleEntityusers;
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationRedirectResponse;
use SymfonyComponentHttpFoundationResponse;
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.trick
这是我在symfony中的登录页面,想显示我在你好{{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 AppBundleEntity;
use DoctrineORMMapping as ORM;
/**
* users
*
* @ORMTable(name="users")
* @ORMEntity(repositoryClass="AppBundleRepositoryusersRepository")
*/
class users
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="user_name", type="string", length=255)
*/
private $userName;
/**
* @var string
*
* @ORMColumn(name="first_name", type="string", length=255)
*/
private $firstName;
/**
* @var string
*
* @ORMColumn(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;
}
}
您有一个错误的缩进来查看问题。。试试这个:
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
。
我不知道这个逻辑对你来说是否正确,但问题是