Kumar Pal Kumar Pal - 2 months ago 30
CSS Question

The controller must return a response (null given). Did you forget to add a return statement somewhere in your controller? please

**DefaultController.php**


This is my DefaultController.php file i am going to simple login page in symfony
i want to display hello with name which i mentioned in controller, plese guide me i am new in 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**


This is my login page in symfony, want to display hello with name which i mentioned in Hello {{name}} this line, please do some correction if needed.

<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;
}
}

Answer Source

you have a bad indentation to see the problem.. try this:

     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');
    }

The problem was that you are inside POST request but there ins't user, so It doesn't enter inside any return.

I have added a new condition else to return in this case.

I don't know if the logic is correct for you, but the problem was that