2015-11-02 123 views
2

我正在创建一个CakePHP 3.0 REST API。我跟着this instructions (routing in the book)并收到json的回应。这是我的代码。CakePHP 3.0:作为json的响应

01 SRC /配置/ 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发送POST请求到该URL

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

输出:

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

但我想JSON出来放不以.json扩展。先谢谢你。

+0

尝试使用'Router :: parseExtensions('json');'而不是'Router :: extensions('json');'? – Maraboc

+2

但是,[3-0-migration-guide says](http://book.cakephp.org/3.0/en/appendices/3-0-migration-guide.html)Router :: parseExtensions()已被删除。使用Cake \ Routing \ Router :: extensions()代替。该方法必须在连接路由之前调用。它不会修改现有的路线。 –

+1

@Maraboc任何我不想为我的网址添加扩展名的方法。我想把没有.json扩展名的JSON响应。 –

回答

0

请求您的数据与proper HTTP accept headerAccept: application/json,RequestHandler应该拿起它然后。

Accept头被HTTP客户端用来告诉服务器他们将接受哪些 内容类型。然后,服务器将发回一个 响应,该响应将包含一个Content-Type标头,告诉客户端 返回的内容实际是什么内容类型。

但是,正如您可能已经注意到的那样,HTTP请求也可以包含 Content-Type标头。为什么?那么,考虑POST或PUT请求。 对于这些请求类型,客户端实际上是将一堆 数据作为请求的一部分发送到服务器,并且Content-Type标头 告诉服务器数据实际是什么(并且因此确定服务器将如何分析 它)。

特别是,对于从HTML表格 提交产生的​​典型POST请求,请求的内容类型通常是 application/x-www-form-urlencoded或multipart/form-data。

5

我有同样的情况,但现在我找到了解决方案。现在我可以把请求的URL没有.json,也可以得到Json数据。

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

use Cake \ Network \ Response;

之后,你需要转换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;echo json_encode($content);_sendContent()函数。

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

+0

这太神奇了..你救了我的一天..谢谢你:) – Poonam

+0

'$ this-> response-> type('json');'做了诡计,谢谢! –

1

如果有人还在寻找容易JSON响应的解决方案这里是一个快速:

此方法添加到您的AppController.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); 

希望可以帮助:)