Symfony控制器必须返回响应(

时间:2017-05-24 14:29:22

标签: php symfony

我试图创建一个登录功能,在做了symfony网站告诉我的操作后,我得到了以下错误; 控制器必须返回响应(

我使用的控制器是;

<?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,
));
}
}

这是我使用的树枝;

{% 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>

我希望有人能对这件事情有所了解

2 个答案:

答案 0 :(得分:1)

尝试

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

而不是renderView

编辑以获得更多说明:

renderView返回渲染视图的字符串表示形式。你可以手动创建一个Response对象,并将该调用的结果设置为内容,但这是render自动为你做的事情。

答案 1 :(得分:1)

检查一下:

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

Controller::renderView()将呈现的视图作为字符串返回,而不是作为响应

尝试改为使用:

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