REST_Controller::response PHP Méthode

response() public méthode

Takes mixed data and optionally a status code, then creates the response
public response ( array | null $data = NULL, integer | null $http_code = NULL, boolean $continue = FALSE )
$data array | null Data to output to the user
$http_code integer | null HTTP status code
$continue boolean TRUE to flush the response to the client and continue running the script; otherwise, exit
    public function response($data = NULL, $http_code = NULL, $continue = FALSE)
    {
        ob_start();
        // If the HTTP status is not NULL, then cast as an integer
        if ($http_code !== NULL) {
            // So as to be safe later on in the process
            $http_code = (int) $http_code;
        }
        // Set the output as NULL by default
        $output = NULL;
        // If data is NULL and no HTTP status code provided, then display, error and exit
        if ($data === NULL && $http_code === NULL) {
            $http_code = self::HTTP_NOT_FOUND;
        } elseif ($data !== NULL) {
            // If the format method exists, call and return the output in that format
            if (method_exists($this->format, 'to_' . $this->response->format)) {
                // Set the format header
                $this->output->set_content_type($this->_supported_formats[$this->response->format], strtolower($this->config->item('charset')));
                $output = $this->format->factory($data)->{'to_' . $this->response->format}();
                // An array must be parsed as a string, so as not to cause an array to string error
                // Json is the most appropriate form for such a datatype
                if ($this->response->format === 'array') {
                    $output = $this->format->factory($output)->{'to_json'}();
                }
            } else {
                // If an array or object, then parse as a json, so as to be a 'string'
                if (is_array($data) || is_object($data)) {
                    $data = $this->format->factory($data)->{'to_json'}();
                }
                // Format is not supported, so output the raw data as a string
                $output = $data;
            }
        }
        // If not greater than zero, then set the HTTP status code as 200 by default
        // Though perhaps 500 should be set instead, for the developer not passing a
        // correct HTTP status code
        $http_code > 0 || ($http_code = self::HTTP_OK);
        $this->output->set_status_header($http_code);
        // JC: Log response code only if rest logging enabled
        if ($this->config->item('rest_enable_logging') === TRUE) {
            $this->_log_response_code($http_code);
        }
        // Output the data
        $this->output->set_output($output);
        if ($continue === FALSE) {
            // Display the data and exit execution
            $this->output->_display();
            exit;
        } else {
            ob_end_flush();
        }
        // Otherwise dump the output automatically
    }

Usage Example

Exemple #1
0
 public function getAction()
 {
     try {
         $lastUpdate = $this->_getParam('lastupdate', date('Y-m-d H:i:s'));
         //            $writer = new Zend_Log_Writer_Stream(APPLICATION_PATH . '/../var/log/logUpdates.log');
         //            $log = new Zend_Log($writer);
         //            $log->info("---------  INICIO GET UPDATE --------------");
         //            $log->info("params : $lastUpdate" . PHP_EOL);
         //            $log->info("------------ FIN GET UPDATE --------------");
         $fields = array('id', 'name', 'picture', 'picture', 'picture', 'paidads', 'flagactive');
         $objCompany = new Application_Entity_RunSql('Company', "lastupdate > '" . urldecode($lastUpdate) . "' AND flagactive=1", null, $fields);
         $objCompany->listed = array();
         $company = $objCompany->listed;
         $fields = array('id', 'idubigeo', 'idcompany', 'name', 'address', 'flagactive');
         $objSubsidiary = new Application_Entity_RunSql('Subsidiary', "lastupdate > '" . urldecode($lastUpdate) . "' AND flagactive=1", null, $fields);
         $objSubsidiary->listed = array();
         $subsidiary = $objSubsidiary->listed;
         $fields = array('id', 'idmovie', 'idsubsidiary', 'schedule3dsubtitle', 'schedule3ddubbing', 'schedulesubtitle', 'scheduledubbing', 'flagactive');
         $objBillboard = new Application_Entity_RunSql('Billboard', "lastupdate > '" . urldecode($lastUpdate) . "' AND flagactive=1", null, $fields);
         $objBillboard->listed = array();
         $billboard = $objBillboard->listed;
         $fields = array('id', 'name', 'idday', 'value', 'idsubsidiary', 'flagactive');
         $objPrice = new Application_Entity_RunSql('Price', "lastupdate > '" . urldecode($lastUpdate) . "' AND flagactive=1", null, $fields);
         $objPrice->listed = array();
         $price = $objPrice->listed;
         $fields = array('id', 'idgenre', 'idubigeo', 'name', 'datepublication', 'premiere', 'synopsis', 'director', 'cast', 'urltrailer', 'picture', 'duration', 'imdb', 'censure', 'flagactive');
         $objMovie = new Application_Entity_RunSql('Movie', "lastupdate > '" . urldecode($lastUpdate) . "' AND flagactive=1", null, $fields);
         $objMovie->listed = array();
         $movie = $objMovie->listed;
         //            $fields = array('id', 'name', 'flagactive');
         //            $objState = new Application_Entity_RunSql('Ubigeo', "lastupdate > '" . urldecode($lastUpdate) . "' AND flagactive=1 AND idcountry = 1
         //                AND idstate != 0 AND idprovince = 0 AND iddistrict = 0 ", null, $fields);
         //            $objState->listed = array();
         //            $state = $objState->listed;
         //            $fields = array('id', 'name', 'flagactive');
         //            $objCountry = new Application_Entity_RunSql('Ubigeo', "lastupdate > '" . urldecode($lastUpdate) . "' AND flagactive=1
         //                AND idstate = 0 AND idprovince = 0 AND iddistrict = 0 ", null, $fields);
         //            $objCountry->listed = array();
         //            $country = $objCountry->listed;
         $fields = array('id', 'idubigeo', 'idcompany', 'name', 'picture', 'flagactive');
         $objAds = new Application_Entity_RunSql('Ads', "lastupdate > '" . urldecode($lastUpdate) . "' AND flagactive=1", null, $fields);
         $objAds->listed = array();
         $ads = $objAds->listed;
         $fields = array('id', 'name', 'flagactive');
         $objGenre = new Application_Entity_RunSql('Genre', "lastupdate > '" . urldecode($lastUpdate) . "' AND flagactive=1", null, $fields);
         $objGenre->listed = array();
         $genre = $objGenre->listed;
         $rpta = array('state' => 1, 'msg' => 'ok', 'data' => array('company' => $company, 'subsidiary' => $subsidiary, 'billboard' => $billboard, 'price' => $price, 'movie' => $movie, 'ads' => $ads, 'genre' => $genre));
     } catch (Exception $exc) {
         $rpta = array('state' => 0, 'msg' => $exc->getMessage());
     }
     parent::response(200, $rpta);
 }