symfony如何选择内容类型?

时间:2010-02-16 08:42:41

标签: php symfony1 content-type

  txt:  text/plain
  js:   [application/javascript, application/x-javascript, text/javascript]
  css:  text/css
  json: [application/json, application/x-json]
  xml:  [text/xml, application/xml, application/x-xml]
  rdf:  application/rdf+xml
  atom: application/atom+xml
  

框架使用它   自动管理Content-Type   响应,基于请求   URI扩展名。

如上所述,.js扩展有3种不同的内容类型,symfony如何选择最终的内容类型?

2 个答案:

答案 0 :(得分:1)

如果没有专门设置内容类型,响应将返回列表中的第一项。

来自sfWebRequest

public function getMimeType($format)
{
  return isset($this->formats[$format]) ? $this->formats[$format][0] : null;
}

$this->formats包含您在问题中指定的mime类型映射的扩展名列表,因为它们是为请求对象指定的。

答案 1 :(得分:0)

我想你可能会在这之后(这个例子使用样式表,但原理是相同的):

// In the view.yml
indexSuccess:
  stylesheets: [main, paper: { media: print }]

// In the Action
$this->getResponse()->addStylesheet('paper', '', array('media' => 'print'));

// In the template
<?php use_stylesheet('paper', '', array('media' => 'print')) ?>

// Resulting View
<link rel="stylesheet" type="text/css" media="print" href="/css/paper.css" />

来自:http://www.symfony-project.org/book/1_2/07-Inside-the-View-Layer