How to set HTTP_HOST for WebTestCases in Symfony2

My application generates some absolute links through $ this-> get ('request') -> getHost (). The problem is that when you try to run test boxes, the following error message appears:

[exception] 500 | Server internal error | Twig_Error_Runtime

[post] During the rendering of the template ("Undefined index: HTTP_HOST"), an exception was thrown in the line ":: base.html.twig" on line 69.

It’s somehow clear to me that when calling my application through the CLI there is no host, but I think there should be a way to prevent Symfony2 from getting this error.

Does anyone know how to get rid of it?

+3
source share
3 answers

, , . Symfony2, :

API- API facebook, SERVER. :

$facebook = $this->container->get('facebook');
$returnUrl = 'http://'.$request->getHost();
$returnUrl .= $this->container->get('router')->generate('_validate_facebook');
$_SERVER['HTTP_HOST'] = $request->getHost();
$_SERVER['REQUEST_URI'] = $request->getRequestUri();

$loginUrl = $facebook->getLoginUrl(array(
        'req_perms' => 'publish_stream',
        'next' => $returnUrl,
        ));

return $loginUrl;

CLI

+2

:

$request = Request::create('http://example.com/path');

HTTP.

+2

, , , , . . X_FORWARDED_HOST, , . , , getHost , HOST, SERVER_NAME , , SERVER_ADDR.

- HOST, , getHost:

$request = $this->get('request');
$request->headers->set('HOST', 'yourhosthere');

$request->getHost(); // Should return yourhosthere

As I said, I'm not sure that this will solve the problem, because the error you mentioned tells us that the template is trying to get the index value HTTP_HOST, but it is not defined. If you look at the methods $request->getHostand $request->getHttpHost, I don’t see anything trying to get a value that has HTTP_HOSTan index, but I could skip it. Could you place the file ::base.html.twigto see if the problem might lie there.

Regards, Matt

+2
source

All Articles