/** * Handles (fakes) redirects for Ajax requests using requestAction() * * @param Controller $controller A reference to the controller * @param string|array $url A string or array containing the redirect location * @param mixed $status HTTP Status for redirect * @param boolean $exit * @return void */ public function beforeRedirect($controller, $url, $status = null, $exit = true) { if (!$this->request->is('ajax')) { return; } foreach ($_POST as $key => $val) { unset($_POST[$key]); } if (is_array($url)) { $url = Router::url($url + array('base' => false)); } if (!empty($status)) { $statusCode = $this->response->httpCodes($status); $code = key($statusCode); $this->response->statusCode($code); } $this->response->body($this->requestAction($url, array('return', 'bare' => false))); $this->response->send(); $this->_stop(); }
/** * Handles (fakes) redirects for Ajax requests using requestAction() * Modifies the $_POST and $_SERVER['REQUEST_METHOD'] to simulate a new GET request. * * @param Controller $controller A reference to the controller * @param string|array $url A string or array containing the redirect location * @param int|array $status HTTP Status for redirect * @param bool $exit Whether to exit script, defaults to `true`. * * @return void */ public function beforeRedirect(Controller $controller, $url, $status = NULL, $exit = TRUE) { if (!$this->request->is('ajax')) { return; } if (empty($url)) { return; } $_SERVER['REQUEST_METHOD'] = 'GET'; foreach ($_POST as $key => $val) { unset($_POST[$key]); } if (is_array($url)) { $url = Router::url($url + array('base' => FALSE)); } if (!empty($status)) { $statusCode = $this->response->httpCodes($status); $code = key($statusCode); $this->response->statusCode($code); } $this->response->body($this->requestAction($url, array('return', 'bare' => FALSE))); $this->response->send(); $this->_stop(); }
/** * Redirects to given $url, after turning off $this->autoRender. * Script execution is halted after the redirect. * * @param string|array $url A string or array-based URL pointing to another location within the app, * or an absolute URL * @param int|array|null $status HTTP status code (eg: 301). Defaults to 302 when null is passed. * @param bool $exit If true, exit() will be called after the redirect * @return void * @triggers Controller.beforeRedirect $this, array($url, $status, $exit) * @link http://book.cakephp.org/2.0/en/controllers.html#Controller::redirect */ public function redirect($url, $status = null, $exit = true) { $this->autoRender = false; if (is_array($status)) { extract($status, EXTR_OVERWRITE); } $event = new CakeEvent('Controller.beforeRedirect', $this, array($url, $status, $exit)); list($event->break, $event->breakOn, $event->collectReturn) = array(true, false, true); $this->getEventManager()->dispatch($event); if ($event->isStopped()) { return; } $response = $event->result; extract($this->_parseBeforeRedirect($response, $url, $status, $exit), EXTR_OVERWRITE); if ($url !== null) { $this->response->header('Location', Router::url($url, true)); } if (is_string($status)) { $codes = array_flip($this->response->httpCodes()); if (isset($codes[$status])) { $status = $codes[$status]; } } if ($status === null) { $status = 302; } $this->response->statusCode($status); if ($exit) { $this->response->send(); $this->_stop(); } }
/** * Redirects to given $url, after turning off $this->autoRender. * Script execution is halted after the redirect. * * @param mixed $url A string or array-based URL pointing to another location within the app, * or an absolute URL * @param integer $status Optional HTTP status code (eg: 404) * @param boolean $exit If true, exit() will be called after the redirect * @return mixed void if $exit = false. Terminates script if $exit = true * @link http://book.cakephp.org/2.0/en/controllers.html#Controller::redirect */ public function redirect($url, $status = null, $exit = true) { $this->autoRender = false; if (is_array($status)) { extract($status, EXTR_OVERWRITE); } $event = new CakeEvent('Controller.beforeRedirect', $this, array($url, $status, $exit)); //TODO: Remove the following line when the events are fully migrated to the CakeEventManager list($event->break, $event->breakOn, $event->collectReturn) = array(true, false, true); $this->getEventManager()->dispatch($event); if ($event->isStopped()) { return; } $response = $event->result; extract($this->_parseBeforeRedirect($response, $url, $status, $exit), EXTR_OVERWRITE); if (function_exists('session_write_close')) { session_write_close(); } if (!empty($status) && is_string($status)) { $codes = array_flip($this->response->httpCodes()); if (isset($codes[$status])) { $status = $codes[$status]; } } if ($url !== null) { $this->response->header('Location', Router::url($url, true)); } if (!empty($status) && ($status >= 300 && $status < 400)) { $this->response->statusCode($status); } if ($exit) { $this->response->send(); $this->_stop(); } }
/** * Tests the httpCodes method * * @expectedException CakeException */ public function testHttpCodes() { $response = new CakeResponse(); $result = $response->httpCodes(); $this->assertEquals(40, count($result)); $result = $response->httpCodes(100); $expected = array(100 => 'Continue'); $this->assertEquals($expected, $result); $codes = array(381 => 'Unicorn Moved', 555 => 'Unexpected Minotaur'); $result = $response->httpCodes($codes); $this->assertTrue($result); $this->assertEquals(42, count($response->httpCodes())); $result = $response->httpCodes(381); $expected = array(381 => 'Unicorn Moved'); $this->assertEquals($expected, $result); $codes = array(404 => 'Sorry Bro'); $result = $response->httpCodes($codes); $this->assertTrue($result); $this->assertEquals(42, count($response->httpCodes())); $result = $response->httpCodes(404); $expected = array(404 => 'Sorry Bro'); $this->assertEquals($expected, $result); //Throws exception $response->httpCodes(array(0 => 'Nothing Here', -1 => 'Reverse Infinity', 12345 => 'Universal Password', 'Hello' => 'World')); }
/** * Tests the httpCodes method * */ public function testHttpCodes() { $response = new CakeResponse(); $result = $response->httpCodes(); $this->assertEquals(39, count($result)); $result = $response->httpCodes(100); $expected = array(100 => 'Continue'); $this->assertEquals($expected, $result); $codes = array(1337 => 'Undefined Unicorn', 1729 => 'Hardy-Ramanujan Located'); $result = $response->httpCodes($codes); $this->assertTrue($result); $this->assertEquals(41, count($response->httpCodes())); $result = $response->httpCodes(1337); $expected = array(1337 => 'Undefined Unicorn'); $this->assertEquals($expected, $result); $codes = array(404 => 'Sorry Bro'); $result = $response->httpCodes($codes); $this->assertTrue($result); $this->assertEquals(41, count($response->httpCodes())); $result = $response->httpCodes(404); $expected = array(404 => 'Sorry Bro'); $this->assertEquals($expected, $result); }
/** * Redirects to given $url, after turning off $this->autoRender. * Script execution is halted after the redirect. * * @param mixed $url A string or array-based URL pointing to another location within the app, * or an absolute URL * @param integer $status Optional HTTP status code (eg: 404) * @param boolean $exit If true, exit() will be called after the redirect * @return mixed void if $exit = false. Terminates script if $exit = true * @link http://book.cakephp.org/2.0/en/controllers.html#Controller::redirect */ public function redirect($url, $status = null, $exit = true) { $this->autoRender = false; if (is_array($status)) { extract($status, EXTR_OVERWRITE); } $response = $this->Components->trigger( 'beforeRedirect', array(&$this, $url, $status, $exit), array('break' => true, 'breakOn' => false, 'collectReturn' => true) ); if ($response === false) { return; } extract($this->_parseBeforeRedirect($response, $url, $status, $exit), EXTR_OVERWRITE); $response = $this->beforeRedirect($url, $status, $exit); if ($response === false) { return; } extract($this->_parseBeforeRedirect($response, $url, $status, $exit), EXTR_OVERWRITE); if (function_exists('session_write_close')) { session_write_close(); } if (!empty($status) && is_string($status)) { $codes = array_flip($this->response->httpCodes()); if (isset($codes[$status])) { $status = $codes[$status]; } } if ($url !== null) { $this->response->header('Location', Router::url($url, true)); } if (!empty($status) && ($status >= 300 && $status < 400)) { $this->response->statusCode($status); } if ($exit) { $this->response->send(); $this->_stop(); } }