views:

325

answers:

2
  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

It is used by the framework to automatically manage the Content-Type of the response, based on the request URI extension.

As above,there are 3 different content types for .js extension,how does symfony choose the final content type?

A: 

I think you may be after this (this example uses stylesheet but principle is the same):

// 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" />

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

Tom
Why don't you take `js` as an example?
As css has only a single content type,it doesn't explain away anything
The principle is the same, check this for example: http://forum.symfony-project.org/index.php/m/90678/ ... otherwise I must be understanding your question incorrectly.
Tom
The only problem is how does symfony choose the one as content type when there are many?
+1  A: 

The response will return the first item in the list if no content type is specifically set.

From sfWebRequest:

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

$this->formats contains the list of extension to mime-type mappings you specified in your question as they are specified for the request object.

Cryo
If you are right,`js` will return `application/javascript` as MIME,but that's not the case
@user198729 What content-type are you seeing instead? Also, is this JS file being served through Symfony? If it's a JS file that exists on your filesystem then your web server is the one responsible for serving the content-type and if you're on a Linux machine with Apache it's most likely coming out of /etc/mime.types.
Cryo
When we run `use_javascript()`,the content-type is obviously `text/javascript`
@user198729 `use_javascript` is a helper method that merely includes a referenced JS file into your response HTML, it doesn't do anything about serving the actual JS file, that's a separate request from the browser after the initial response is given. If you're speaking to the `type="text/javascript"` portion of the `<script>` tag that's rendered, that is a hard-coded value when rendering javascript files and is unaffected by the request's format configuration.
Cryo