CakePHP: calling testAction to a json-returning me

2019-09-02 06:13发布

问题:

What am I missing here? Here is my controller code:

public function calculate() {
    $this->set(array(
        "route" => array("A" => 1, "B" => 2),
        "_serialize" => array("route")
    ));

    return;
}

Here is a line from my routes.php file:

Router::parseExtensions();

Here is my test code:

    $result = $this->testAction("/itinerary/calculate.json", array(
        "method" => "POST", 
        "return" => "contents"
    ));

This code throws

MissingViewException: View file "C:\xampp\htdocs\fiver\app\View\Itinerary\calculate.ctp" is missing.

I am obviously missing something here. Please help. Another test for another controller with JSON works just fine

回答1:

Got it. CakePHP requires the RequestHandler component to be explicitly added to the controller for the extensions to work. I've added this line, it started to work

public $components = array('RequestHandler');


回答2:

If you dont have/want a view for your controller you can simply add

$this->autoRender = false;

// EDIT: Only working if you dont want an output but thats not the case