satooshi/symfony2contrib-http-foundation-extra-bundle

Symfony2 contrib http foundation extra bundle

0.1.1 2013-01-21 11:21 UTC

This package is not auto-updated.

Last update: 2024-09-14 11:38:21 UTC


README

安装

请见Packagist.

注解使用

此包包含以下注解。

  • @File: 强制下载文件。
  • @Json: 从控制器结果创建JsonResponse。
    • 内容类型是
      • application/json 对于 json 响应
      • text/javascript 对于 jsonp 响应
# Acme/Bundle/Controller/SiteController.php

use Contrib\Bundle\HttpFoundationExtraBundle\Configuration\File;
use Contrib\Bundle\HttpFoundationExtraBundle\Configuration\Json;

/**
 * @Route("/site")
 */
class SiteController
{
    /**
     * @Route("/csv", name="site_csv")
     * @Method("GET")
     * @File(filename = "test.csv", charset = "Shift_JIS")
     */
    public function csvAction()
    {
        $content = 'item1,item2';

        return array(
            'content' => $content,

            // read file in FileResponse and ignore content if this was set to controller result
            //'path' => './test.csv', 
        );
    }

    /**
     * @Route("/json", name="site_json")
     * @Method("GET")
     * @Json
     */
    public function jsonAction()
    {
    	// result response
    	// {"prop1":"value1","prop2":"value2"}
        return array(
        	'prop1' => 'value1',
        	'prop2' => 'value2'
        );
    }

    /**
     * @Route("/jsonp", name="site_jsonp")
     * @Method("GET")
     * @Json(callbackName = "jsoncallback")
     */
    public function jsonpAction()
    {
    	// request
    	// /site/jsonp?jsoncallback=mycallback
    	// response
    	// mycallback({"prop1":"value1","prop2":"value2"});
        return array(
        	'prop1' => 'value1',
        	'prop2' => 'value2'
        );
    }
}

@File 参数

  • filename: 如果 "content" 设置为控制器结果则必需。如果 "path" 设置则可选。用于 "Content-Disposition" HTTP 头
  • charset: 可选字符编码。用于 "Content-Type" HTTP 头
  • mimeType: 可选 MIME 类型。默认值是 "application/octet-stream"。用于 "Content-Type" HTTP 头

@Json 参数

  • callbackName: 可选回调请求参数名称(默认值:callback)。
  • serialize: 可选布尔值,表示是否使用序列化器(jms/serializer)。
  • serializeGroups: 可选数组,用于序列化组。

todo

  • 避免 HTTP 头冲突,如果它已经被设置
  • 添加特定文件类型注解支持(@Plain, @Csv, @Pdf, @Zip 等)
  • 添加渲染模板功能,与 @Template 相同
  • 添加 DependencyInjection/Cofiguration 以支持默认参数配置
  • 添加单元测试