发布于 2015-08-27 16:49:36 | 174 次阅读 | 评论: 0 | 来源: 网络整理
Every Request has a “format” (e.g. html, json), which is used
to determine what type of content to return in the Response. In fact,
the request format, accessible via
getRequestFormat(),
is used to set the MIME type of the Content-Type header on the Response
object. Internally, Symfony contains a map of the most common formats (e.g.
html, json) and their associated MIME types (e.g. text/html,
application/json). Of course, additional format-MIME type entries can
easily be added. This document will show how you can add the jsonp format
and corresponding MIME type.
The FrameworkBundle registers a subscriber that will add formats to incoming requests.
All you have to do is to configure the jsonp format:
# app/config/config.yml
framework:
request:
formats:
jsonp: 'application/javascript'
<!-- app/config/config.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony
http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"
>
<framework:config>
<framework:request>
<framework:format name="jsonp">
<framework:mime-type>application/javascript</framework:mime-type>
</framework:format>
</framework:request>
</framework:config>
</container>
// app/config/config.php
$container->loadFromExtension('framework', array(
'request' => array(
'formats' => array(
'jsonp' => 'application/javascript',
),
),
));
小技巧
You can also associate multiple mime types to a format, but please note that the preferred one must be the first as it will be used as the content type:
# app/config/config.yml
framework:
request:
formats:
csv: ['text/csv', 'text/plain']
<!-- app/config/config.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony
http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"
>
<framework:config>
<framework:request>
<framework:format name="csv">
<framework:mime-type>text/csv</framework:mime-type>
<framework:mime-type>text/plain</framework:mime-type>
</framework:format>
</framework:request>
</framework:config>
</container>
// app/config/config.php
$container->loadFromExtension('framework', array(
'request' => array(
'formats' => array(
'jsonp' => array(
'text/csv',
'text/plain',
),
),
),
));