概述 快速入门 教程 手册 最佳实践 组件 参考 贡献

发布于 2015-08-27 16:49:36 | 136 次阅读 | 评论: 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.

Configure your New Format

The FrameworkBundle registers a subscriber that will add formats to incoming requests.

All you have to do is to configure the jsonp format:

  • YAML
    # app/config/config.yml
    framework:
        request:
            formats:
                jsonp: 'application/javascript'
    
  • XML
    <!-- 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>
    
  • PHP
    // 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:

  • YAML
    # app/config/config.yml
    framework:
        request:
            formats:
                csv: ['text/csv', 'text/plain']
    
  • XML
    <!-- 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>
    
  • PHP
    // app/config/config.php
    $container->loadFromExtension('framework', array(
        'request' => array(
            'formats' => array(
                'jsonp' => array(
                    'text/csv',
                    'text/plain',
                ),
            ),
        ),
    ));
    
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务