【问题标题】:How to specify Content-type when using Recess framework with Smarty如何在 Smarty 中使用 Recess 框架时指定 Content-type
【发布时间】:2012-01-13 19:24:02
【问题描述】:

我目前正在使用带有 Smarty 模板引擎的 PHP Recess 框架。在我的控制器中,我的代码类似于:

/**
* !View Smarty
* !RespondsWith Smarty
* !Prefix Views: templates/, Routes: /
*/

class XHomeController extends Controller {

    /** !Route GET */
    function index()
    {
            $this->title = "Some title...";
    }

}

并且,在相应的 Smarty 视图中,我照常引用 {$title}

除了 Android 浏览器(在我的 2.3 Nexus One、3.2 平板电脑以及 Android 模拟器上)之外,该视图在所有浏览器中都能正确呈现。我认为我已经将问题追溯到 Smarty 视图被渲染并发送到没有 Content-type 的浏览器这一事实。

使用http://web-sniffer.net/,我注意到响应中的 Content-type 为空。

使用 Smarty 时如何在 Recess 中指定 Content-type?我尝试将 header('Content-type: text/html') 添加到控制器中的方法,但这不起作用。

知道我做错了什么吗?

【问题讨论】:

    标签: php http http-headers smarty recess


    【解决方案1】:

    我想在凹槽/框架/视图中查看 SmartyView 代码。该类应该有一个 canRespondWith() 方法,该方法将验证视图是否可以使用某个 MIMEType 进行响应。例如:

    class XmlView extends AbstractView {
    
        public function canRespondWith(Response $response) {
                return 'xml' === $response->request->accepts->format();
        }
    }
    

    如果返回 true,则将使用 XmlView。在 AbstractView 类中,sendHeaders() 方法会设置 Content-Type:

    protected function sendHeadersFor(Response $response) {
        header('HTTP/1.1 ' . ResponseCodes::getMessageForCode($response->code));
    
        $format = $response->request->accepts->format();
        header('Content-Type: ' . MimeTypes::preferredMimeTypeFor($format));
        /* ... */
    }
    

    查看recess/http/MimeTypes.class.php 以查看“xml”如何以正确的标头响应。您还需要查看 SmartyView 以查看您返回的 mimetype 以查看将设置的标题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-18
      • 2011-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-27
      • 2012-03-29
      • 2011-12-19
      相关资源
      最近更新 更多