You could re-think your code this way
public function actionTest()
{
try {
// Here: everything went ok. So before returning JSON, you can setup HTTP status code too
$rtn = array("id", "3", "name", "John");
http_response_code(200);
print json_encode($rtn);
}
catch (SomeException $ex) {
$rtn = array("id", "3", "error", "something wrong happened");
http_response_code(500);
print json_encode($rtn);
}
}
Basically, before stream the output (the JSON data), you can set the HTTP status code by http_response_code($code)
function.
And about your additional question in comment, yes, printing the JSON data is the correct way.