Php laravel如何为所有json响应添加前缀以防止json注入

Php laravel如何为所有json响应添加前缀以防止json注入,php,json,laravel-4,Php,Json,Laravel 4,此主题已被请求 没有任何答复,所以我再试一次 我试过了 Route::filter('protectionJson',function($route,$request ,$response) { if($request->ajax() && ($response instanceof \Illuminate\Http\JsonResponse)){ return ")]}',\n".json_encode($response->getData(

此主题已被请求

没有任何答复,所以我再试一次

我试过了

Route::filter('protectionJson',function($route,$request ,$response)
{
    if($request->ajax() && ($response instanceof \Illuminate\Http\JsonResponse)){
       return ")]}',\n".json_encode($response->getData());
    }
});
Route::get('user', array('as' => 'base.user.index', 'uses' => 'App\Controllers\UserController@index'))->before('hasAccess:users')->after('protectionJson');


但它不起作用,我的意思是,我一直使用标准json格式。

如果您想在响应前添加数据,可以使用响应对象
getContent()
方法访问响应数据

Route::filter('json.protect',function($route,$request,$response = null)
{
    if($response instanceof \Illuminate\Http\JsonResponse) {
        $json = ")]}',\n" . $response->getContent();
        return $response->setContent($json);
    }
});
然后,可以使用
after
属性将其附加到管线

Route::get('/test', array('after' =>'json.protect', function()
{
    $test = array(
        "foo" => "bar",
        "bar" => "foo",
    );

    return Response::json($test);
}));
或者,如果您不想将筛选器附加到每个路由,那么也可以使用
App::after
hook

App::after(function($request, $response)
{
    if($response instanceof \Illuminate\Http\JsonResponse) {
        $json = ")]}',\n" . $response->getContent();
        return $response->setContent($json);
    }
});

上述代码的哪个部分不起作用?到目前为止你试过调试什么?@Jeemusu我一直都是标准的json格式。
App::after(function($request, $response)
{
    if($response instanceof \Illuminate\Http\JsonResponse) {
        $json = ")]}',\n" . $response->getContent();
        return $response->setContent($json);
    }
});