tbn / json-annotation-bundle
此包已被 废弃 并不再维护。未建议替代包。
此包最新版本(1.2.1)的许可证信息不可用。
添加动作的 JSON 注释
1.2.1
2017-05-16 08:48 UTC
This package is auto-updated.
Last update: 2024-02-07 03:36:46 UTC
README
JsonAnnotationBundle 允许您在控制器中使用 Json 注释
用法
在控制器中使用 @Json() 注释
#配置
一些参数是可选的
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
响应
正常响应
它是一个包含属性 'success'(值为 true)和属性 'data'(包含控制器返回的数组)的 JSON 流
异常响应
它是一个包含属性 'success'(值为 false)和属性 'message'(包含错误)的 JSON 流
示例
使用 composer 导入包
"tbn/json-annotation-bundle": "dev-master"
在 AppKernel 中导入包
new tbn\JsonAnnotationBundle\JsonAnnotationBundle()
正常响应示例
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');
}
}
将返回一个 JSON 流
'success' => true
'data' => ['data1' => 'value1', 'data2' => 'value2']
异常响应
use tbn\JsonAnnotationBundle\Configuration\Json;
class DefaultController extends Controller
{
/**
* The main view
*
* @Route("/someroute")
* @Json()
*
* @return array
*/
public function somerouteAction()
{
throw \Exception('some error occured');
}
}
将返回一个 JSON 流
'success' => false
'message' => 'some error occured'
事件
在 JSON 响应开始时触发一个前钩事件。例如,可以用来验证令牌。
some_bundle.listener.json_token_validation_listener:
class: "some_bundle\\Listener\\JsonTokenValidationListener"
tags:
- { name: kernel.event_listener, event: json.pre_hook, method: onJsonPrehook }
此方法接受一个类型为 JsonPreHookEvent 的参数。
public function onJsonPrehook(JsonPreHookEvent $jsonPreHookEvent)