]> dev.renevier.net Git - syj.git/blobdiff - application/controllers/helpers/SyjApi.php
allow uploading a route as a file
[syj.git] / application / controllers / helpers / SyjApi.php
index 86ea00e3ff1cdc6fa6355843e81b5d164bddc496..bfb97927d1a2aeb97d51573869a4a040308d2646 100644 (file)
@@ -8,6 +8,7 @@ class Syj_Controller_Action_Helper_SyjApi extends Zend_Controller_Action_Helper_
     protected $_checkIfNoneMatch = false;
     protected $_body = '';
     protected $_code = 200;
+    protected $_redirect = '';
 
     public function init() {
         $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
@@ -27,6 +28,12 @@ class Syj_Controller_Action_Helper_SyjApi extends Zend_Controller_Action_Helper_
         $this->_contentType = $contentType;
     }
 
+    public function setBodyJson($data) {
+        $this->setBody(json_encode($data))
+             ->setContentType('application/json');
+        return $this;
+    }
+
     public function setBody($body) {
         $this->_body = (string)$body;
         return $this;
@@ -45,6 +52,20 @@ class Syj_Controller_Action_Helper_SyjApi extends Zend_Controller_Action_Helper_
         return $this->_code;
     }
 
+    public function setRedirect($url, $code = 0) {
+        $this->_redirect = (string)$url;
+        if (is_numeric($code) && (int)$code >= 300 && (int)$code < 400) {
+            $this->_code;
+        } else if (!isset($this->_code)) {
+            $this->code = 301;
+        }
+        return $this;
+    }
+
+    public function getRedirect() {
+        return $this->_redirect;
+    }
+
     public function setCheckIfNoneMatch($check) {
         $this->_checkIfNoneMatch = (boolean)$check;
         return $this;
@@ -74,6 +95,10 @@ class Syj_Controller_Action_Helper_SyjApi extends Zend_Controller_Action_Helper_
             $response->setHeader ('Etag', $etag);
         }
 
+        if ($this->_redirect) {
+            $response->setHeader ('Location', $this->_redirect);
+        }
+
         $response->setHttpResponseCode($this->_code)
                  ->setBody($this->_body);
     }