REST und Zend Framework 2
class IndexController extends AbstractRestfulController{
private function _getResponseWithHeader(){
$response = $this->getResponse();
$response->getHeaders()
//make can accessed by *
->addHeaderLine('Access-Control-Allow-Origin','*')
//set allow methods
->addHeaderLine('Access-Control-Allow-Methods','POST PUT DELETE GET')
//change content-type
->addHeaderLine('Content-Type', 'application/json; charset=utf-8');
return $response;
}
private function _createJSONResponse($obj){
$json=new XWJSONConverter();
$response = $this->_getResponseWithHeader()->setContent($json->encode($obj));
return $response;
}
public function indexAction(){
$entity=$this->getServiceManager()->get("entityDAO")->load(1);
return $this->_createJSONResponse($entity);
}
}