2017 © Pedro Peláez
 

library json-annotation-bundle

add the json annotation for an action

image

tbn/json-annotation-bundle

add the json annotation for an action

  • Tuesday, May 16, 2017
  • by thomasbeaujean
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1,238 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 4 % Grown

The README.md

JsonAnnotationBundle

The JsonAnnotationBundle permits to use an annotation Json for your controller, (*1)

Usage

Use the annotation @Json() in your controller, (*2)

Configuration

Some parameters are optionnals:, (*3)

    json_annotation:
            exception_code: 500 #the http code used for the exception
            data_key: "data" # the key used to contains the data, it can be directly at the root, using the "" parameter
            exception_message_key: "message" #the key for the exeception message
            success_key: "success" #the key for the success (that is true is the result is ok, false for an exception)
            post_query_back: false #do we send back the post parameters
            post_query_key: "query" #the key for the post back parameters
            enable_authentication_error: false #A json response is sent back is the user is not authenticated or not granted

The reponse

The normal response

It is a json stream with the property 'success' with the true value and the property 'data' containing the array returned in the controller, (*4)

The exception response

It is a json stream with the property 'success' with the false value and the property 'message' containing the error, (*5)

Examples

Import the bundle using composer

"tbn/json-annotation-bundle": "dev-master"

Import the bundle in your AppKernel

new tbn\JsonAnnotationBundle\JsonAnnotationBundle()

The normal response Example

use tbn\JsonAnnotationBundle\Configuration\Json;

class DefaultController extends Controller
{

     /**
      * The main view
      *
      * @Route("/someroute")
      * @Json()
      *
      * @return array
      */
     public function somerouteAction()
     {
         return array('data1' => 'value1', 'data2' => 'value2');
     }
  }

It will send back a json stream, (*6)

 'success' => true
 'data'    => ['data1' => 'value1', 'data2' => 'value2']

The exception response

use tbn\JsonAnnotationBundle\Configuration\Json;, (*7)

 class DefaultController extends Controller
 {
     /**
      * The main view
      *
      * @Route("/someroute")
      * @Json()
      *
      * @return array
      */
     public function somerouteAction()
     {
         throw \Exception('some error occured');
     }
 }

It will send back a json stream, (*8)

 'success' => false
 'message'    => 'some error occured'

Events

A pre-hook event is dispatched at the beginning of the json response. It can be used to validate a token for example., (*9)

some_bundle.listener.json_token_validation_listener:
    class: "some_bundle\\Listener\\JsonTokenValidationListener"
    tags:
        - { name: kernel.event_listener, event: json.pre_hook, method: onJsonPrehook }

The method has one argument of type JsonPreHookEvent., (*10)

public function onJsonPrehook(JsonPreHookEvent $jsonPreHookEvent)

The Versions

16/05 2017

dev-master

9999999-dev

add the json annotation for an action

  Sources   Download

13/04 2015

1.2.0

1.2.0.0

add the json annotation for an action

  Sources   Download

20/03 2015

1.1.0

1.1.0.0

add the json annotation for an action

  Sources   Download

19/03 2015

1.0.2

1.0.2.0

add the json annotation for an action

  Sources   Download

17/03 2015

dev-rename-namespace

dev-rename-namespace

add the json annotation for an action

  Sources   Download

The Development Requires