2017 © Pedro Peláez
 

symfony-bundle symfony2contrib-http-foundation-extra-bundle

Symfony2 contrib http foundation extra bundle

image

satooshi/symfony2contrib-http-foundation-extra-bundle

Symfony2 contrib http foundation extra bundle

  • Monday, January 21, 2013
  • by satooshi
  • Repository
  • 1 Watchers
  • 1 Stars
  • 15 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

ContribHttpFoundationExtraBundle

Install

See Packagist., (*1)

Annotation Usage

This bundle contains the following annotations., (*2)

  • @File: force to download a file.
  • @Json: create JsonResponse from controller result.
    • Content-Type is
      • application/json for json response
      • text/javascript for jsonp response
# 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 parameters

  • filename: required if "content" was set to controller result. optional if "path" was set. used for "Content-Disposition" HTTP header
  • charset: optional charactor encoding. used for "Content-Type" HTTP header
  • mimeType: optional mime type. default value is "application/octet-stream". used for "Content-Type" HTTP header

@Json parameters

  • callbackName: optional callback request parameter name (default value: callback).
  • serialize: optional boolean whether to use serializer (jms/serializer).
  • serializeGroups: optional array to use serialize group.

todo

  • avoid HTTP header conflict if it has already been set
  • add supecific file type annotation support (@Plain, @Csv, @Pdf, @Zip, and so on)
  • add rendering template functionality as the same as @Template
  • add DependencyInjection/Cofiguration to support default parameter configuration
  • add unit test

The Versions