gpt4 book ai didi

php - Controller 必须返回响应(给定为空)。您是否忘记在 Controller 中的某处添加返回语句?请

转载 作者:太空宇宙 更新时间:2023-11-04 01:37:36 26 4
gpt4 key购买 nike

**DefaultController.php**

这是我的 DefaultController.php 文件,我将转到 symfony 中的简单登录页面我想用我在 Controller 中提到的名字显示你好,请指导我,我是 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;
}
}

最佳答案

你有一个错误的缩进来查看问题..试试这个:

     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 以返回。

我不知道你的逻辑是否正确,但问题在于

关于php - Controller 必须返回响应(给定为空)。您是否忘记在 Controller 中的某处添加返回语句?请,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45915846/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com