Symfony 控制器必须返回一个响应( [英] Symfony The controller must return a response (

查看:24
本文介绍了Symfony 控制器必须返回一个响应(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建登录功能,但在执行 symfony 网站告诉我的操作后,出现以下错误;控制器必须返回响应 (

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 (

我使用的控制器是;

<?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>

希望有人能解释一下这个问题

I hope someone can shine some light on this matter

推荐答案

尝试

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

代替renderView

instead of renderView

编辑以获得更多说明:

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

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.

这篇关于Symfony 控制器必须返回一个响应(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆