服务器下载渲染的 Twig 文件而不是在浏览器中显示它

Server Downloads Rendered Twig File instead of show it in the Browser

我的应用程序出现问题,在我的开发环境中一切正常。但是在我的托管服务器上,php 被解释但它没有在浏览器中显示它,它只是下载一个文件。

视图在 slim/twig 中呈现,当我做出 json 响应而不是返回视图时,一切正常。

 $apiEndpoints = json_decode(file_get_contents(__DIR__.'/ApiController/endpoints.json'));

    return $this->view->render($response, 'index.twig', [
        'apiEndpoints' => $apiEndpoints,
    ]);

页面是这一页Application

我在这台服务器上的另一个应用程序中有相同的环境,这没有问题,我已经降级了 slim/twig。

也许有人在 http header 中看到了问题?我认为它看起来应该如此。

我不知道你的代码和架构,但是视图为什么要做渲染?树枝渲染视图很有意义。所以这个:

return $this->view->render(

应该看起来像这样:

return $twig->render($this->view, array(...

服务器正在发送 application/x-httpd-php 的响应,您的浏览器不知道如何处理它。 HTML 的内容类型应为 text/html.

$ curl --include http://app.uhc-scorpions.ch/

HTTP/1.1 200 OK
Date: Fri, 17 Feb 2017 06:34:52 GMT
Server: Apache/2.2.22 (Debian)
X-Powered-By: PHP/7.0.10
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Set-Cookie: userSession_Session=2quum1alcu5siipkim6qmdqul2; path=/; HttpOnly
Content-Length: 6310
Content-Type: application/x-httpd-php

感谢您的所有帖子,看来我对 slim 框架太蠢了:)

不工作:

 $response->withHeader('Content-type', 'text/html');
    return $this->view->render($response, 'index.twig', [
        'apiEndpoints' => $apiEndpoints,
    ]);

正在工作:

 return $this->view->render($response->withHeader('Content-type', 'text/html'), 'index.twig', [
        'apiEndpoints' => $apiEndpoints,
    ]);

现在可以使用了