无法访问控制器响应的正确内容

Cannot access to proper content of controller response

我正在为我的 Symfony3 应用程序编写功能测试,但遇到无法接收控制器返回的数据的问题。控制器的每次调用仍然返回 301 重定向。我注意到,每次调用控制器都会调用 RedirectResponse class 和 setTargetUrl 方法,该方法负责重定向到正确的控制器。当我以用户身份使用我的应用程序时,一切都很好,但爬虫只有 returns 有关重定向的信息。

这是一个例子:

我的测试方法:

public function testListController()
{
    $client = static::createClient();
    $crawler = $client->request('GET', '/list');
    print_r($client->getResponse());
}

方法returns:

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="refresh" content="1;url=https://localhost/list/" />

    <title>Redirecting to http://localhost/list/</title>
</head>
<body>
    Redirecting to <a href="http://localhost/list/">https://localhost/list/</a>.
</body>

为什么我无法访问页面的正确内容?每个钩子都被禁用了,在访问控制器之前我没有调用任何方法。

此行为是由于路由定义末尾的斜杠所致。所以试试这个:

$crawler = $client->request('GET', '/list/');

而不是:

$crawler = $client->request('GET', '/list');

希望对您有所帮助