8

cookbookRequestHandlerの部分を読みました。、などがありますが、ありません。isXml()isRss()isJson()

リクエストが JSON かどうかを確認する他の方法はありますか?

したがって、URL が存在する場合mysite.com/products/view/1.jsonは JSON データが提供されますが、それがない.json場合は HTML ビューが提供されます。

ありがとう

4

6 に答える 6

4

独自の検出器を作成できます。参照: http://book.cakephp.org/2.0/en/controllers/request-response.html#inspecting-the-request

たとえば、 AppController.php で

public function beforeFilter() {
  $this->request->addDetector(
    'json',
    [
      'callback' => [$this, 'isJson']
    ]
  );
  parent::beforeFilter();
}

public function isJson() {
  return $this->response->type() === 'application/json';
}

これで使用できます:

$this->request->is('json'); // or
$this->request->isJson();
于 2013-06-27T08:06:56.197 に答える
1
class TestController extends Controller {

    public $autoRender = false;

    public function beforeFilter() {
        $this->request->addDetector('json', array('env' => 'CONTENT_TYPE', 'pattern' => '/application\/json/i'));
        parent::beforeFilter();
    }

    public function index() {
        App::uses('HttpSocket', 'Network/Http');

        $url = 'http://localhost/myapp/test/json';
        $json = json_encode(
            array('foo' => 'bar'),
            JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP
        );
        $options = array('header' => array('Content-Type' => 'application/json'));

        $request = new HttpSocket();
        $body = $request->post($url, $json, $options)->body;

        $this->response->body($body);
    }

    public function json() {

        if ($this->request->isJson()) {
            $data = $this->request->input('json_decode');
            $value = property_exists($data, 'foo') ? $data->foo : '';  
        }

        $body = (isset($value) && $value === 'bar') ? 'ok' : 'fail';
        $this->response->body($body);
    }
}
于 2014-09-09T04:04:08.963 に答える
1

@Schlaefer氏に感謝します。私はあなたのコメントを読んで試してみました。

//AppController.php

function beforeFilter() {
        $this->request->addDetector(
                'json', [
            'callback' => [$this, 'isJson']
                ]
        );
        parent::beforeFilter();
       ...
    }


public function isJson() {
        return $this->response->type() === 'application/json';
    }
//TasksController.php

public $components = array('Paginator', 'Flash', Session','RequestHandler');

// Get tasks 関数はすべてのタスクを json 形式で返します

public function getTasks() {
        $limit = 20;
        $conditions = array();
        if (!empty($this->request->query['status'])) {
            $conditions = ['Task.status' => $this->request->query['status']];
        }
        if (!empty($this->request->query['limit'])) {
            $limit = $this->request->query['limit'];
        }
        $this->Paginator->settings = array('limit' => $limit, 'conditions' => $conditions);

        $tasks = $this->paginate();

        if ($this->request->isJson()) {

            $this->set(
array(
                'tasks' => $tasks,
                '_serialize' => array('tasks')
            ));
        }
    }
于 2016-04-28T02:56:59.517 に答える