代码之家  ›  专栏  ›  技术社区  ›  Izaid Sanchez

通过外部类修改Slim 3中的响应对象

  •  0
  • Izaid Sanchez  · 技术社区  · 8 年前

    我的slim应用程序有一个问题,我想发送json响应,但需要自定义标题。我的代码如下:

    指数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();
    

    app/libs/api。反恐精英。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);
        }
    }
    

    我的路线文件(app/routes.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
    

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

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

    你能帮我告诉我这些方法是好是坏吗。

    2 回复  |  直到 8 年前
        1
  •  0
  •   Ramy hakam    8 年前

    只需将此函数添加到Middelware文件中即可

     $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');
        });
    
        2
  •  0
  •   Izaid Sanchez    8 年前