thomasbeaujean / 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:47:39 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 的 JSON 流,以及包含控制器返回数组的属性 'data'
异常响应
它是一个包含属性 'success' 且值为 false 的 JSON 流,以及包含错误的属性 'message'
示例
使用 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)