Mateusz Ravage Mateusz Ravage - 1 month ago 13
PHP Question

Cannot access to proper content of controller response

I'm writing functional tests for my Symfony3 application and I have the problem that I cannot receive data returned by controller. Every call of controller still returning 301 redirect. I've noticed, that every call of controller calls also RedirectResponse class with setTargetUrl method which is responsible for redirecting to proper controller. When I use my application as user it's everything good, but crawler only returns info about redirect.

Here is an example:

My testing method:

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


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


Why I cannot access to proper content of the page? Every hooks has been disabled, and there aren't any method that I call before accessing to the controller.

Answer

This behaviour is due to a trailing slash at the end of your route definition. So try this:

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

Instead of:

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

Hope this help