/** * Handles (fakes) redirects for Ajax requests using requestAction() * * @param Event $event The Controller.beforeRedirect event. * @param string|array $url A string or array containing the redirect location * @param \Cake\Network\Response $response The response object. * @return void */ public function beforeRedirect(Event $event, $url, Response $response) { $request = $this->request; if (!$request->is('ajax')) { return; } if (empty($url)) { return; } if (is_array($url)) { $url = Router::url($url + ['_base' => false]); } $controller = $event->subject(); $response->body($controller->requestAction($url, ['return', 'bare' => false, 'environment' => ['REQUEST_METHOD' => 'GET']])); $response->send(); $response->stop(); }
public function beforeRedirect(Event $event, $url, Response $response) { // $logged_in = is_int($this->Controller->Auth->user('id')); $login_action = false; if (is_string($url)) { if (stripos($url, 'login') !== false) { $login_action = true; } } elseif (is_array($url)) { if ($url['action'] == 'login') { $login_action = true; } } if ($this->request->is('service') && !$logged_in && $login_action) { $this->data = [self::SUCCESS_KEY => false, self::ENABLE_KEY => true, self::CODE_KEY => 401, self::MESSAGE_KEY => __('You are not logged in!')]; $this->Controller->render(); $response->statusCode(201); $response->send(); $response->stop(); if (session_id()) { session_write_close(); } } }
/** * Handles (fakes) redirects for Ajax requests using requestAction() * Modifies the $_POST and $_SERVER['REQUEST_METHOD'] to simulate a new GET request. * * @param Event $event The Controller.beforeRedirect event. * @param string|array $url A string or array containing the redirect location * @param \Cake\Network\Response $response The response object. * @return void */ public function beforeRedirect(Event $event, $url, $response) { 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)); } $controller = $event->subject(); $response->body($controller->requestAction($url, array('return', 'bare' => false))); $response->send(); $response->stop(); }
/** * Parses a string URL into an array. Parsed URLs will result in an automatic * redirection * * @param string $url The URL to parse * @return bool False on failure */ public function parse($url) { $params = parent::parse($url); if (!$params) { return false; } if (!$this->response) { $this->response = new Response(); } $redirect = $this->redirect; if (count($this->redirect) === 1 && !isset($this->redirect['controller'])) { $redirect = $this->redirect[0]; } if (isset($this->options['persist']) && is_array($redirect)) { $redirect += ['pass' => $params['pass'], 'url' => []]; if (is_array($this->options['persist'])) { foreach ($this->options['persist'] as $elem) { if (isset($params[$elem])) { $redirect[$elem] = $params[$elem]; } } } $redirect = Router::reverse($redirect); } $status = 301; if (isset($this->options['status']) && ($this->options['status'] >= 300 && $this->options['status'] < 400)) { $status = $this->options['status']; } $this->response->header(['Location' => Router::url($redirect, true)]); $this->response->statusCode($status); $this->response->send(); $this->response->stop(); }