CakePHP 3.0: Response as json


CakePHP 3.0: Response as json

我正在创建一个CakePHP 3.0 REST API。我按照此说明(书中的路由(并收到了 json 的响应。这是我的代码。

01 src/config/rout.php

Router::extensions('json');

02 src/controler/UsersController.php

  public function view($id = null) {
    $data = array('status' => 'o','status_message' => 'ok' );
    $this->set('data', $data);
    $this->set('_serialize', 'data');  
}

03 向此 URL 发送发布请求

http://domain.com/users/view.json

输出:

{
    "status": "o",
    "status_message": "ok"
}

但是我想在没有 .json 扩展名的情况下让 json 放出来。 提前谢谢你。

我有同样的情况,但现在我找到了解决方案。现在我可以放置没有 .json 的请求 url,并且也可以在响应中获取 Json 数据。

在应用程序控制器中添加网络响应,这将处理您的响应。

使用蛋糕''网络''响应;

之后,您需要在数组中转换 Json 输入,因此请将此getJsonInput()函数放在您的AppController中并在initialize()中调用它

public function getJsonInput() {
        $data = file_get_contents("php://input");
        $this->data = (isset($data) && $data != '') ? json_decode($data, true) : array();
    }

现在在您的控制器中,您拥有 $this->data 中的所有发布数据,因此您可以访问所有输入。这是一个例子:

class UsersController extends AppController {
    public function index() {
        if ($this->request->is('post')) {
            //pr($this->data);    //here is your all inputs
           $this->message = 'success';
           $this->status = true;
        }
        $this->respond();
    }
}

现在在函数结束时,您需要调用respond() AppController 中定义

public function respond() {  
        $this->response->type('json');  // this will convert your response to json
        $this->response->body([
                    'status' => $this->status,
                    'code' => $this->code,
                    'responseData' => $this->responseData,
                    'message'=> $this->message,
                    'errors'=> $this->errors,
                ]);   // Set your response in body
        $this->response->send();  // It will send your response
        $this->response->stop();  // At the end stop the response
    }

AppController中的所有变量定义为

public $status = false;
public $message = '';
public $responseData = array();
public $code = 200;
public $errors = '';

还有一件事要做:

Response.php (/vendor/cakephp/cakephp/src/Network/Response.php) 您需要在 586 处编辑一行 echo $content; _sendContent()功能中的echo json_encode($content);

就是这样。现在您可以将请求 url 设置为 domain_name/project_name/users/index .

如果有人仍在寻找简单的 json 响应解决方案,这里有一个快速的解决方案:

将此方法添加到应用控制器.php

public function jsonResponse($responseData = [], $responseStatusCode = 200) {
    Configure::write('debug', 0);
    $this->response->type('json');
    $this->response->statusCode($responseStatusCode);
    $this->response->body(json_encode($responseData));
    $this->response->send();
    $this->render(false,false);
}

您可以在任何简单的操作中使用它

$data = ['status' => 'ok', 'foo' => 'bar'];
$this->jsonResponse($data);

和另一个例子

$data = ['status' => 'failed', 'bar' => 'foo'];
$this->jsonResponse($data, 404);

希望对:)有所帮助

使用适当的 HTTP 接受标头请求您的数据 Accept: application/json ,然后 RequestHandler 应该会拾取它。

HTTP 客户端使用 Accept 标头告诉服务器什么 他们将接受的内容类型。然后服务器将发回一个 响应,这将包括一个内容类型标头,告知客户端 返回内容的内容类型实际上是什么。

但是,您可能已经注意到,HTTP 请求也可以包含 内容类型标头。为什么?好吧,考虑一下 POST 或 PUT 请求。 使用这些请求类型,客户端实际上是在发送一堆 数据作为请求的一部分发送到服务器,以及 Content-Type 标头 告诉服务器数据实际上是什么(从而确定如何 服务器将解析它(。

特别是,对于由 HTML 表单生成的典型 POST 请求 提交,请求的内容类型通常为以下任一 application/x-www-form-urlencoding or multipart/form-data.