Example #1
0
/**
 * Return the Response
 * @param mixed $response
 * @return mixed
 */
function zbase_response($response)
{
    // HTTP/1.1 204 No Content
    //	$apiResponse = zbase()->json()->getVariable('api');
    //	if(!empty($apiResponse) && $apiResponse instanceof \Zbase\Exceptions\HttpException && $apiResponse->getStatusCode() == 204)
    //	{
    //		$response->header('HTTP/1.1 204 No Content');
    //		return $response;
    //	}
    $returnNoContent = '';
    $errorResponse = false;
    $xmlResponse = false;
    $responseFormat = zbase_response_format();
    if (zbase_is_json()) {
        $responseFormat = 'json';
    }
    if ($responseFormat == 'json' || zbase_request_is_ajax()) {
        $jsonResponse = true;
    }
    if ($responseFormat == 'xml') {
        $xmlResponse = true;
    }
    if (zbase_is_angular_template()) {
        $responseFormat = 'html';
        $jsonResponse = false;
    }
    if (!empty($jsonResponse)) {
        $formId = zbase_request_input('_formId', null);
        if (!empty($formId)) {
            zbase()->json()->setVariable('_formId', $formId);
        }
        zbase()->json()->setVariable('_route', zbase_route_name());
        zbase()->json()->setVariable('_package', zbase_view_template_package());
        $code = 200;
        if ($response instanceof \RuntimeException) {
            $code = $response->getStatusCode();
            zbase()->json()->setVariable('statusCode', $code);
            if ($code !== 200) {
                $errorResponse = true;
                zbase()->json()->setVariable('statusMessage', $response->getStatusMessage());
            }
        }
        /**
         * its ajax, but method is GET
         */
        if (empty($errorResponse)) {
            $tokenResponse = zbase_request_input('token', zbase_request_query_input('token', false));
            if (!$tokenResponse) {
                zbase()->json()->setVariable('_token', zbase_csrf_token());
            }
        }
        zbase()->json()->setVariable('_alerts', ['errors' => zbase_alerts('error'), 'messages' => zbase_alerts('success'), 'info' => zbase_alerts('info'), 'warning' => zbase_alerts('warning')]);
        $jsonScripts = zbase()->json()->getVariable('_html_script');
        $jsonScriptReturn = '';
        if (!empty($jsonScripts)) {
            foreach ($jsonScripts as $jScripts) {
                if (!empty($jScripts[0])) {
                    $jScripts[0] = str_replace(array('<script type="text/javascript">', '</script>'), '', $jScripts[0]);
                    $jsonScriptReturn .= $jScripts[0] . ';';
                }
            }
        }
        zbase()->json()->setVariable('_html_script', str_replace(';;', ';', $jsonScriptReturn));
        $forceResponse = zbase_request_input('forceResponse', zbase_request_query_input('forceResponse', false));
        /**
         * JSONP Callback
         */
        $jsonCallback = zbase_request_query_input('callback', zbase_request_query_input('jsonp', false));
        if (!$forceResponse) {
            zbase_alerts_render();
            if (!empty($jsonCallback)) {
                return response()->json(zbase()->json()->getVariables(), $code)->setCallback($jsonCallback);
            } else {
                return response()->json(zbase()->json()->getVariables(), $code);
            }
        }
    }
    if ($response instanceof \RuntimeException) {
        if ($response->getStatusCode() == '302') {
            if (zbase_is_json()) {
                zbase_alerts_render();
                if (!empty($jsonCallback)) {
                    return response()->json(zbase()->json()->getVariables(), 302)->setCallback($jsonCallback);
                } else {
                    return response()->json(zbase()->json()->getVariables(), 302);
                }
            }
        }
        return $response->render(zbase_request(), $response);
    }
    /**
     * REsponse with a javascript code
     */
    if ($responseFormat == 'javascript') {
        $response = \Response::make($response, 200);
        $response->header('Content-Type', 'application/javascript');
    }
    return $response;
}
Example #2
0
/**
 * Check if current route is $name
 * @param string $name The name to test
 * @return boolean
 */
function zbase_route_name_is($name)
{
    return zbase_route_name() == $name;
}