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
This commit is contained in:
Samuel Georges 2016-07-23 11:57:15 +10:00
parent 584f2871b0
commit 7baea87068
2 changed files with 18 additions and 8 deletions

View File

@ -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);
}

View File

@ -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);
}