redirectBack() 'default' => array('Director', 'redirectBack'), ); /** * @param RequestHandler $controller * @param Array $callbacks */ function __construct($callbacks = array()) { $this->callbacks = $callbacks; } /** * Out of the box, the handler "CurrentForm" value, which will return the rendered form. * Non-Ajax calls will redirect back. * * @param SS_HTTPRequest $request * @param array $extraCallbacks List of anonymous functions or callables returning either a string * or SS_HTTPResponse, keyed by their fragment identifier. The 'default' key can * be used as a fallback for non-ajax responses. * @return SS_HTTPResponse */ public function respond(SS_HTTPRequest $request, $extraCallbacks = array()) { // Prepare the default options and combine with the others $callbacks = array_merge( array_change_key_case($this->callbacks, CASE_LOWER), array_change_key_case($extraCallbacks, CASE_LOWER) ); $response = new SS_HTTPResponse(); $responseParts = array(); if($fragmentStr = $request->getHeader('X-Pjax')) { $fragments = explode(',', strtolower($fragmentStr)); foreach($fragments as $fragment) { if(isset($callbacks[$fragment])) { $responseParts[$fragment] = call_user_func($callbacks[$fragment]); } else { throw new SS_HTTPResponse_Exception("X-Pjax = '$fragment' not supported for this URL.", 400); } } if(count($responseParts) == 1) { $response->setBody(array_pop($responseParts)); } else { if($request->getHeader('Accept') != 'text/json') { throw new SS_HTTPResponse_Exception( 'Multiple comma-separated fragments can only be returne with an "Accept: text/json" header', 400 ); } $response->setBody(Convert::raw2json($responseParts)); $response->addHeader('Content-Type', 'text/json'); } } else { if($request->isAjax()) throw new SS_HTTPResponse_Exception("Ajax requests to this URL require an X-Pjax header.", 400); $response->setBody(call_user_func($callbacks['default'])); } return $response; } /** * @param String $fragment * @param Callable $callback */ public function setCallback($fragment, $callback) { $this->callbacks[$fragment] = $callback; } }