Resources/contao/api/index.php
public function run()
        {

            // Set default headers for api
            header('Content-Type: application/json');

            // Maintenance mode
            if ($GLOBALS['TL_CONFIG']['maintenanceMode'] && !BE_USER_LOGGED_IN) {
                header('HTTP/1.1 503 Service Unavailable');
                exit;
            }

            // Get path
            $arrFragments = $this->getFragmentsFromUrl();

            // Stop on empty path
            if (empty($arrFragments)) {
                header('HTTP/1.1 400 Bad Request');
                exit;
            }

            // Extract api endpoint
            $strApiEndpoint = array_shift($arrFragments);

            // check if its a test-call
            if ($strApiEndpoint == 'c4g_apicheck_ajax') {
                if (!$arrFragments[0] || array_key_exists($arrFragments[0], $GLOBALS['TL_API'])) {
                    return true;
                } else {
                    header('HTTP/1.1 501 Not Implemented');
                    exit;
                }
            }

            // Stop if no matching endpoint is found
            if (!array_key_exists($strApiEndpoint, $GLOBALS['TL_API'])) {
                header('HTTP/1.1 400 Bad Request');
                exit;
            }

            $blnUseCache = false;
            $blnOutputFromCache = false;

            if (!\Config::get('debugMode') && (\Config::get('cacheMode') == 'both' || \Config::get('cacheMode') == 'server') && in_array($strApiEndpoint, $GLOBALS['CON4GIS']['USE_CACHE']['SERVICES']))
            {
                $blnUseCache = true;
            }

            if (is_array($GLOBALS['CON4GIS']['USE_CACHE']['PARAMS']))
            {
                foreach ($GLOBALS['CON4GIS']['USE_CACHE']['PARAMS'] as $key=>$arrValues)
                {
                    if (\Input::get($key) && in_array(\Input::get($key), $arrValues))
                    {
                        $blnUseCache = true;
                    }
                }
            }


            if ($blnUseCache)
            {
                // check for cached data
                if ($strResponse = \con4gis\CoreBundle\Resources\contao\classes\C4GApiCache::getCacheData($strApiEndpoint, $arrFragments))
                {
                    $blnOutputFromCache = true;
                }
            }

            // Generate the result

            // check for jsonp request
            if (\Input::get('callback'))
            {
                echo \Input::get('callback') . '(';
            }

            if ($blnOutputFromCache)
            {
                echo $strResponse;
            }
            else
            {

                // Create the api endpoint handler
                $objHandler = new $GLOBALS['TL_API'][$strApiEndpoint]();

                $strResponse = $objHandler->generate($arrFragments);
                if (is_array($strResponse)) {
                    header("Content-Type: " . $strResponse['type']);
                    $strResponse = $strResponse['data'];
                }

                if ($blnUseCache)
                {
                    // write data into cache
                    \con4gis\CoreBundle\Resources\contao\classes\C4GApiCache::putCacheData($strApiEndpoint, $arrFragments, $strResponse);
                }

                echo $strResponse;

            }


            // check for jsonp request
            if (\Input::get('callback'))
            {
                echo ');';
            }

        }
public function run()
        {

            // Set default headers for api
            header('Content-Type: application/json');

            // Maintenance mode
            if ($GLOBALS['TL_CONFIG']['maintenanceMode'] && !BE_USER_LOGGED_IN) {
                header('HTTP/1.1 503 Service Unavailable');
                exit;
            }

            // Get path
            $arrFragments = $this->getFragmentsFromUrl();

            // Stop on empty path
            if (empty($arrFragments)) {
                header('HTTP/1.1 400 Bad Request');
                exit;
            }

            // Extract api endpoint
            $strApiEndpoint = array_shift($arrFragments);

            // check if its a test-call
            if ($strApiEndpoint == 'c4g_apicheck_ajax') {
                if (!$arrFragments[0] || array_key_exists($arrFragments[0], $GLOBALS['TL_API'])) {
                    return true;
                } else {
                    header('HTTP/1.1 501 Not Implemented');
                    exit;
                }
            }

            // Stop if no matching endpoint is found
            if (!array_key_exists($strApiEndpoint, $GLOBALS['TL_API'])) {
                header('HTTP/1.1 400 Bad Request');
                exit;
            }

            $blnUseCache = false;
            $blnOutputFromCache = false;

            if (!\Config::get('debugMode') && (\Config::get('cacheMode') == 'both' || \Config::get('cacheMode') == 'server') && in_array($strApiEndpoint, $GLOBALS['CON4GIS']['USE_CACHE']['SERVICES']))
            {
                $blnUseCache = true;
            }

            if (is_array($GLOBALS['CON4GIS']['USE_CACHE']['PARAMS']))
            {
                foreach ($GLOBALS['CON4GIS']['USE_CACHE']['PARAMS'] as $key=>$arrValues)
                {
                    if (\Input::get($key) && in_array(\Input::get($key), $arrValues))
                    {
                        $blnUseCache = true;
                    }
                }
            }


            if ($blnUseCache)
            {
                // check for cached data
                if ($strResponse = \con4gis\CoreBundle\Resources\contao\classes\C4GApiCache::getCacheData($strApiEndpoint, $arrFragments))
                {
                    $blnOutputFromCache = true;
                }
            }

            // Generate the result

            // check for jsonp request
            if (\Input::get('callback'))
            {
                echo \Input::get('callback') . '(';
            }

            if ($blnOutputFromCache)
            {
                echo $strResponse;
            }
            else
            {

                // Create the api endpoint handler
                $objHandler = new $GLOBALS['TL_API'][$strApiEndpoint]();

                $strResponse = $objHandler->generate($arrFragments);
                if (is_array($strResponse)) {
                    header("Content-Type: " . $strResponse['type']);
                    $strResponse = $strResponse['data'];
                }

                if ($blnUseCache)
                {
                    // write data into cache
                    \con4gis\CoreBundle\Resources\contao\classes\C4GApiCache::putCacheData($strApiEndpoint, $arrFragments, $strResponse);
                }

                echo $strResponse;

            }


            // check for jsonp request
            if (\Input::get('callback'))
            {
                echo ');';
            }

        }
Run the controller