-1

I was trying to make a log in function and after doing what the symfony website told me to I get the following error; The controller must return a response (

And the controller I use is;

<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class SecurityController extends Controller
{
/**
 * @Route("/login", name="login")
 */
public function loginAction(Request $request){
$authenticationUtils = $this->get('security.authentication_utils');

// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();

// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();

return $this->renderView('login.html.twig', array(
    'last_username' => $lastUsername,
    'error'         => $error,
));
}
}

and this is the twig I use;

{% if error %}
<div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}

<form action="{{ path('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" />

{#
    If you want to control the URL the user
    is redirected to on success (more details below)
    <input type="hidden" name="_target_path" value="/artikel/alle" />
#}

<button type="submit">login</button>
</form>

I hope someone can shine some light on this matter

Colin
  • 21
  • 2
  • 9

2 Answers2

1

Try

return $this->**render**('login.html.twig', array(
    'last_username' => $lastUsername,
    'error'         => $error,
));

instead of renderView

Edit for more clarification:

renderView returns the string representation of the rendered view. you could manually create a Response object and set the result of that call as content, but that's what renderdoes for you automatically.

Joe
  • 2,356
  • 10
  • 15
  • thank you so much! Would you know how to reroute after succes perhaps? – Colin May 24 '17 at 14:34
  • Take a look at these 2 methods: `redirect` or `redirectToRoute` – Joe May 24 '17 at 14:35
  • @Colin if you want to redirect after the user had successsful logged in, you have to define it in your security file. https://stackoverflow.com/questions/8374753/redirect-after-login-in-symfony-2 – Grechka Vassili May 24 '17 at 14:37
  • @Colin Good hint concerning a static redirectTarget. For a dynamic target an AuthenticationSuccessListener would be the keyword to look out for :) – Joe May 24 '17 at 14:41
1

Check this:

-http://api.symfony.com/2.8/Symfony/Bundle/FrameworkBundle/Controller/Controller.html#method_renderView

Controller::renderView() returns the rendered View as a string, not as a Response

Try to use instead:

return $this->render('login.html.twig', array(
  'last_username' => $lastUsername,
  'error'         => $error,
));
bnxuq
  • 214
  • 1
  • 9