2017-08-30 27 views
0

我有一个问题,我的苗条应用程序,我想发送json响应,但与定制的标题。我的代码是像如下:通过外部类修改Slim 3中的响应对象

的index.php

require 'vendor/autoload.php'; 
require 'app/config.php'; 
require 'app/libs/api.cs.php'; 

$app = new Slim\App( 
    [ 
     "settings" => $config, 
     "apics" => function() { return new APIHelper(); } //This is a class that contain a "helper" for api responses 
    ] 
); 

require 'app/dependences.php'; 
require 'app/middleware.php'; 
require 'app/loader.php'; 
require 'app/routes.php'; 

// Run app 
$app->run(); 

应用程序/库/ api.cs.php(在 “助手”)

<?php 
class APIHelper 
{ 
    public function sendResponse($response, $status='success' ,$code = 200, $message = "", $data = null) 
    { 
     $arrResponse = array(); 
     $arrResponse['status'] = $status; 
     $arrResponse['code'] = $code; 
     $arrResponse['message'] = $message; 
     $arrResponse['data'] = $data; 
     return $response 
     ->withHeader('Access-Control-Allow-Origin', '*') 
     ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization, AeroTkn') 
     ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS') 
     ->withHeader('Content-Type','application/json') 
     ->withHeader('X-Powered-By','My API Server') 
     ->withJson($arrResponse,$code); 
    } 
} 

我的路线文件(应用程序/路线。 PHP)

$app->group('/foo', function() { 
    $this->get('', function ($req, $res, $args) { 
    return $this->apics->sendResponse($res, 'success' ,200, "Foo API Index By Get", null); 
}); 

    $this->post('', function ($req, $res, $args) { 
    try{ 
     $oBody = $req->getParsedBody(); 
     return $this->apics->sendResponse($res, 'success' ,200, "Foo API POST Response", $oBody);   
     } 
     catch(\Exception $ex){ 
     return $this->apics->sendResponse($res, 'error' ,500, "Process Error", array('error' => $ex->getMessage())); 
    } 
    }); 
}); 

当我尝试运行我的请求主体的应用程序,其结果是后续: 头:

connection →Keep-Alive 
content-type →text/html 
date →Wed, 30 Aug 2017 02:22:56 GMT 
keep-alive →timeout=2, max=500 
server →Apache 
transfer-encoding →chunked 

机构(如返回简单的文本,而不是JSON编码)

{"status":"success","code":200,"message":"Foo API POST Response","data":{"one":"1", "two":"2"}} 

我试图把这个类作为一个中间件,但我对这些问题的一些困惑。

你能帮我告诉我,如果这些方法很好,或者我不好。

感谢大家,我希望你的答案!美好的一天

+0

你是什么意思,当你说'当我试图运行我的应用与请求身体?你的代码在我的机器上工作正常。 – Nima

+0

Hi @Nima。 对不起,我没有写好这个部分,我会说当我向我的应用程序调用任何方法时,在上面的示例中,我使用请求主体中的所谓后发送数据。 –

回答

0

使用中间件是你的问题了理想的答案

只需添加此功能在您的middeleware文件

$app->add(function ($req, $res, $next) { 
     $response = $next($req, $res); 
     return $response 
     ->withHeader('Access-Control-Allow-Origin', 'http://mysite') 
     ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization') 
     ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); 
     ->withHeader('Content-Type','application/json'); 
     ->withHeader('X-Powered-By','My API Server'); 
    }); 
+0

嗨@Ramy hakam。感谢您的评论!哟知道吗?这很奇怪,因为这些听起来像是你说的理想的解决方案,我试过同样的解决方案,但是不同之处在于你排除了json模式准备,我已经分离了我的代码,将头文件定制到中间件和json我已经把它留在了服务中。但是自定义响应头仍然没有出现,“传输编码”的值为“分块”。 –

0

我找到了“错误”是一个幼儿园的问题哈哈哈,我已经下载我的所有代码都来自web服务器,在我的机器上进行测试,结果相同,但是我发现我的所有文件在启动时都有奇怪的字符,所以我将文件重新保存为utf-8,问题就解决了。小细节,可以创造头痛!感谢Nica和Ramy。 Ramy:解决方案非常好,现在代码更加组织化,我采取了这种做法。美好的一天。

相关问题