Skip to content

Commit

Permalink
Added options to respond() (#21)
Browse files Browse the repository at this point in the history
* CORS option added.

* Style fix.

* Updated class check.

* Updated $options param definition.

* Update ApiApplication.php
  • Loading branch information
cokencorn authored and wilsonge committed Jul 17, 2017
1 parent 8e622ca commit 8e24a19
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions libraries/src/CMS/Application/ApiApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

namespace Joomla\CMS\Application;

defined('_JEXEC') or die;
defined('JPATH_PLATFORM') or die;

use Joomla\Application\Web\WebClient;
use Joomla\CMS\Router\ApiRouter;
use Joomla\DI\Container;
use Joomla\Registry\Registry;
use Joomla\CMS\Router\ApiRouter;

/**
* Joomla! API Application class
Expand Down Expand Up @@ -93,16 +93,29 @@ protected function render()
/**
* Method to send the application response to the client. All headers will be sent prior to the main application output data.
*
* @param array $options An optional argument to enable CORS. (Temporary)
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
protected function respond()
protected function respond($options = array())
{
$this->setBody(json_encode($this->getBody()));

// Set the Joomla! API signature
$this->setHeader('X-Powered-By', 'JoomlaAPI/1.0', true);

if (array_key_exists('cors', $options))
{
// Enable CORS (Cross-origin resource sharing)
$this->setHeader('Access-Control-Allow-Origin', '*', true);
$this->setHeader('Access-Control-Allow-Headers', 'Authorization');
}

// Parent function can be overridden later on for debugging.
parent::respond();

}

/**
Expand Down Expand Up @@ -134,7 +147,7 @@ public function getTemplate($params = false)
*/
protected function route()
{
$uri = \JUri::getInstance();
$uri = \JUri::getInstance();
$router = static::getRouter();

// Trigger the onBeforeApiRoute event.
Expand Down

0 comments on commit 8e24a19

Please sign in to comment.