From 7baea87068e496d83d6afd46fccee699779cb2ad Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Sat, 23 Jul 2016 11:57:15 +1000 Subject: [PATCH] Allow AJAX handlers to pipe objects to Laravel Treat non scalar, non array, non RedirectResponse, non null as a Laravel compatible response This brings AJAX handlers in line with page cycles, which do the same thing if the response is not a string. However in AJAX we should treat any scalar as a "result", not just a string. For all else (where not null), let Laravel handle it as a custom response object -- could be a model for serialization, or anything compatible. Importantly to note when a custom redirect is sent, October's workflow is completely wiped out, so any partial updates, redirects, etc. will not occur. This is a normal and fair expectation. Fixes #1784 --- modules/backend/classes/Controller.php | 13 +++++++++---- modules/cms/classes/Controller.php | 13 +++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/modules/backend/classes/Controller.php b/modules/backend/classes/Controller.php index 6117f1a1f..747e2e8d4 100644 --- a/modules/backend/classes/Controller.php +++ b/modules/backend/classes/Controller.php @@ -426,11 +426,12 @@ class Controller extends Extendable } /* - * If the handler returned a redirect, process it so framework.js knows to redirect - * the browser and not the request! + * If the handler returned a redirect, process the URL and dispose of it so + * framework.js knows to redirect the browser and not the request! */ if ($result instanceof RedirectResponse) { $responseContents['X_OCTOBER_REDIRECT'] = $result->getTargetUrl(); + $result = null; } /* * No redirect is used, look for any flash messages @@ -448,14 +449,18 @@ class Controller extends Extendable /* * If the handler returned an array, we should add it to output for rendering. - * If it is a string, add it to the array with the key "result". + * If it is a scalar, add it to the array with the key "result". + * Otherwise, pass it to Laravel as a response object. */ if (is_array($result)) { $responseContents = array_merge($responseContents, $result); } - elseif (is_string($result)) { + elseif (is_scalar($result)) { $responseContents['result'] = $result; } + elseif ($result !== null) { + return $result; + } return Response::make()->setContent($responseContents); } diff --git a/modules/cms/classes/Controller.php b/modules/cms/classes/Controller.php index e19040474..c10595084 100644 --- a/modules/cms/classes/Controller.php +++ b/modules/cms/classes/Controller.php @@ -638,23 +638,28 @@ class Controller } /* - * If the handler returned a redirect, process it so framework.js knows to redirect - * the browser and not the request! + * If the handler returned a redirect, process the URL and dispose of it so + * framework.js knows to redirect the browser and not the request! */ if ($result instanceof RedirectResponse) { $responseContents['X_OCTOBER_REDIRECT'] = $result->getTargetUrl(); + $result = null; } /* * If the handler returned an array, we should add it to output for rendering. - * If it is a string, add it to the array with the key "result". + * If it is a scalar, add it to the array with the key "result". + * Otherwise, pass it to Laravel as a response object. */ if (is_array($result)) { $responseContents = array_merge($responseContents, $result); } - elseif (is_string($result)) { + elseif (is_scalar($result)) { $responseContents['result'] = $result; } + elseif ($result !== null) { + return $result; + } return Response::make($responseContents, $this->statusCode); }