redirect() public method

Redirects to given $url, after turning off $this->autoRender.
public redirect ( string | array $url, integer $status = 302 ) : Response | null
$url string | array A string or array-based URL pointing to another location within the app, or an absolute URL
$status integer HTTP status code (eg: 301)
return Cake\Network\Response | null
Beispiel #1
1
 /**
  * Calculates the page to redirect to.
  *
  * @param int $topic_id
  * @param int $post_id
  * @param bool $return
  * @return mixed
  */
 public function goToPage($topic_id = null, $post_id = null, $return = false)
 {
     $topic = ClassRegistry::init('Forum.Topic')->getById($topic_id);
     $slug = !empty($topic['Topic']['slug']) ? $topic['Topic']['slug'] : null;
     // Certain page
     if ($topic_id && $post_id) {
         $posts = ClassRegistry::init('Forum.Post')->getIdsForTopic($topic_id);
         $perPage = Configure::read('Forum.settings.postsPerPage');
         $totalPosts = count($posts);
         if ($totalPosts > $perPage) {
             $totalPages = ceil($totalPosts / $perPage);
         } else {
             $totalPages = 1;
         }
         if ($totalPages <= 1) {
             $url = array('plugin' => 'forum', 'controller' => 'topics', 'action' => 'view', $slug, '#' => 'post-' . $post_id);
         } else {
             $posts = array_values($posts);
             $flips = array_flip($posts);
             $position = $flips[$post_id] + 1;
             $goTo = ceil($position / $perPage);
             $url = array('plugin' => 'forum', 'controller' => 'topics', 'action' => 'view', $slug, 'page' => $goTo, '#' => 'post-' . $post_id);
         }
         // First post
     } else {
         if ($topic_id && !$post_id) {
             $url = array('plugin' => 'forum', 'controller' => 'topics', 'action' => 'view', $slug);
             // None
         } else {
             $url = $this->Controller->referer();
             if (!$url || strpos($url, 'delete') !== false) {
                 $url = array('plugin' => 'forum', 'controller' => 'forum', 'action' => 'index');
             }
         }
     }
     if ($return) {
         return $url;
     }
     return $this->Controller->redirect($url);
 }
Beispiel #2
1
 /**
  * Handle unauthorized access attempt
  *
  * @param \Cake\Controller\Controller $controller A reference to the controller object
  * @return \Cake\Network\Response
  * @throws \Cake\Network\Exception\ForbiddenException
  */
 protected function _unauthorized(Controller $controller)
 {
     if ($this->_config['unauthorizedRedirect'] === false) {
         throw new ForbiddenException($this->_config['authError']);
     }
     $this->flash($this->_config['authError']);
     if ($this->_config['unauthorizedRedirect'] === true) {
         $default = '/';
         if (!empty($this->_config['loginRedirect'])) {
             $default = $this->_config['loginRedirect'];
         }
         $url = $controller->referer($default, true);
     } else {
         $url = $this->_config['unauthorizedRedirect'];
     }
     return $controller->redirect($url);
 }
 /**
  * Logic triggered after comment was successfully saved.
  *
  * @param \Cake\Datasource\EntityInterface $comment Comment that was just saved
  * @return void
  */
 protected function _afterSave(EntityInterface $comment)
 {
     $successMessage = $this->config('successMessage');
     if (is_callable($successMessage)) {
         $successMessage = $successMessage($comment, $this->_controller);
     }
     $this->_controller->Flash->success($successMessage, ['key' => 'commentsForm']);
     if ($this->config('redirectOnSuccess')) {
         $redirectTo = $this->config('redirectOnSuccess') === true ? $this->_controller->referer() : $this->config('redirectOnSuccess');
         $this->_controller->redirect($redirectTo);
     }
 }
Beispiel #4
0
 /**
  * Set redirect by request data action.
  *
  * @param array $options
  * @return \Cake\Network\Response|void
  */
 public function setRedirect(array $options = [])
 {
     $_options = ['apply' => [], 'savenew' => ['action' => 'add'], 'save' => ['action' => 'index'], 'default' => ['action' => 'index']];
     $options = Hash::merge($_options, $options);
     $url = $options['default'];
     if ($action = $this->request->data('action')) {
         list($controllerName, $requestAction) = $this->splitRequestAction();
         if (isset($options[$requestAction])) {
             $url = $options[$requestAction];
         }
     }
     return $this->_controller->redirect($url);
 }
Beispiel #5
0
 /**
  * Count user failed login.
  *
  * @param Controller $controller
  * @return \Cake\Network\Response|void
  */
 protected function _countFailedLogin(Controller $controller)
 {
     $config = PluginConfig::getInstance()->getGroup('union_community');
     $cacheName = 'failed_' . $controller->request->data('username');
     $cacheConfig = 'auth';
     if ($controller->request->param('prefix') == 'admin') {
         $cacheConfig .= '_admin';
     }
     $cacheValue = Cache::read($cacheName, $cacheConfig);
     $maxAuth = $config->params()->get('max_auth', 5);
     $cookieVal = $controller->Cookie->read('fail.auth');
     if ($cacheValue >= $maxAuth || $cookieVal >= $maxAuth) {
         $controller->Flash->error(__d('community', 'You have reached maximum limit for failed login attempts. Please try again after a few minutes.'));
         return $controller->redirect($controller->Auth->config('loginAction'));
     }
 }
 /**
  * test that beforeRedirect callback returning false in controller
  *
  * @return void
  */
 public function testRedirectBeforeRedirectListenerReturnFalse()
 {
     $Response = $this->getMock('Cake\\Network\\Response', ['stop', 'header']);
     $Controller = new Controller(null, $Response);
     $Controller->eventManager()->attach(function ($event, $url, $response) {
         return false;
     }, 'Controller.beforeRedirect');
     $Controller->response->expects($this->never())->method('stop');
     $Controller->response->expects($this->never())->method('header');
     $Controller->response->expects($this->never())->method('statusCode');
     $result = $Controller->redirect('http://cakephp.org');
     $this->assertNull($result);
 }
 /**
  * redirect method
  *
  * @param mixed $url
  * @param mixed $status
  * @return void|\Cake\Network\Response
  */
 public function redirect($url, $status = null)
 {
     $this->testUrl = Router::url($url);
     return parent::redirect($url, $status);
 }
 /**
  * maintenance redirect logic
  *
  * @return mixed|void
  */
 public function beforeFilter()
 {
     if (defined('PHPUNIT_TESTSUITE')) {
         return;
     }
     $maintenancePage = Environment::read('MAINTENANCE_PAGE_REDIRECT_URL');
     $currentUrl = $this->request->here;
     $accessibleUrls = explode('|', Environment::read('MAINTENANCE_ACCESSIBLE_URLS'));
     $accessibleUrls[] = $maintenancePage;
     if (!self::isMaintenanceActive()) {
         // if maintenance is not active but maintenance page is requested -> redirect to default page
         if (in_array($currentUrl, $accessibleUrls) && substr($maintenancePage, -strlen($currentUrl)) === $currentUrl) {
             $maintenanceBasePage = Environment::read('MAINTENANCE_BASE_URL');
             return $this->_controller->redirect($maintenanceBasePage);
         }
         return;
     }
     $cookieName = Environment::read('MAINTENANCE_COOKIE_NAME');
     $cookieExists = $this->_controller->Cookie->read($cookieName) != null;
     if ($cookieExists) {
         return;
     }
     $headerActive = Environment::read('MAINTENANCE_HEADER_ACTIVE');
     $headerName = Environment::read('MAINTENANCE_HEADER_NAME');
     $headerValue = Environment::read('MAINTENANCE_HEADER_VALUE');
     $successUrl = Environment::read('MAINTENANCE_PASSWORD_SUCCESS_URL');
     if ($headerActive && !empty($this->request->header($headerName)) && $this->request->header($headerName) == $headerValue) {
         $this->_controller->Cookie->write($cookieName, true);
         return $this->_controller->redirect($successUrl);
     }
     $passwordUrl = Environment::read('MAINTENANCE_PASSWORD_URL');
     $accessibleUrls[] = $passwordUrl;
     if (!in_array($currentUrl, $accessibleUrls)) {
         return $this->_controller->redirect($maintenancePage);
     } elseif ($currentUrl != $passwordUrl) {
         return;
     }
     $user = Environment::read('MAINTENANCE_USER');
     $password = Environment::read('MAINTENANCE_PASSWORD');
     if ($currentUrl == $passwordUrl) {
         if (!isset($_SERVER['PHP_AUTH_USER'])) {
             header('WWW-Authenticate: Basic realm="Maintenance Realm"');
             header('HTTP/1.0 401 Unauthorized');
             echo 'Unauthorized';
             exit;
         } else {
             if ($_SERVER['PHP_AUTH_USER'] == $user && $_SERVER['PHP_AUTH_PW'] == $password) {
                 $this->_controller->Cookie->write($cookieName, true);
                 return $this->_controller->redirect($successUrl);
             }
             return $this->_controller->redirect($maintenancePage);
         }
     }
     return $this->_controller->redirect($maintenancePage);
 }
Beispiel #9
-1
 public function testRedirectBeforeRedirectListenerReturnResponse()
 {
     $Response = $this->getMockBuilder('Cake\\Network\\Response')->setMethods(['stop', 'header', 'statusCode'])->getMock();
     $Controller = new Controller(null, $Response);
     $newResponse = new Response();
     $Controller->eventManager()->on('Controller.beforeRedirect', function ($event, $url, $response) use($newResponse) {
         return $newResponse;
     });
     $result = $Controller->redirect('http://cakephp.org');
     $this->assertSame($newResponse, $result);
 }
 /**
  * @param \Cake\Event\Event $event
  * @throws \Exception
  * @return \Cake\Network\Response|null
  */
 public function beforeFilter(Event $event)
 {
     $this->Controller = $event->subject();
     // For debug overwrite
     if (($debug = $this->request->session()->read('Setup.debug')) !== null) {
         Configure::write('debug', $debug);
     }
     if (!isset($this->Controller->Flash)) {
         throw new Exception('Flash component missing in AppController setup.');
     }
     // maintenance mode?
     $overwrite = Configure::read('Maintenance.overwrite');
     if ($overwrite) {
         // if this is reachable, the whitelisting is enabled and active
         $message = __d('setup', 'Maintenance mode active - your IP %s is in the whitelist.', $overwrite);
         $this->Controller->Flash->warning($message);
     }
     // The following is only allowed with proper clearance
     if (!$this->isAuthorized()) {
         return null;
     }
     // maintenance mode
     if ($this->Controller->request->query('maintenance') !== null) {
         $mode = $this->Controller->request->query('maintenance') ? __d('setup', 'activated') : __d('setup', 'deactivated');
         $result = $this->setMaintenance($this->Controller->request->query('maintenance'));
         if ($result !== false) {
             $this->Controller->Flash->success(__d('setup', 'Maintenance mode {0}', $mode));
         } else {
             $this->Controller->Flash->error(__d('setup', 'Maintenance mode not {0}', $mode));
         }
         return $this->Controller->redirect($this->_cleanedUrl('maintenance'));
     }
     // debug mode
     if ($this->Controller->request->query('debug') !== null) {
         $result = $this->setDebug($this->Controller->request->query('debug'));
         if ($result !== false) {
             $this->Controller->Flash->success(__('debug set to %s', $this->Controller->request->query('debug')));
         } else {
             $this->Controller->Flash->error(__('debug not set'));
         }
         return $this->Controller->redirect($this->_cleanedUrl('debug'));
     }
     // clear cache
     if ($this->Controller->request->query('clearcache') !== null) {
         $result = $this->clearCache($this->Controller->request->query('clearcache'));
         if ($result !== false) {
             $this->Controller->Flash->success(__('cache cleared'));
         } else {
             $this->Controller->Flash->error(__('cache not cleared'));
         }
         return $this->Controller->redirect($this->_cleanedUrl('clearcache'));
     }
     // clear session
     if ($this->Controller->request->query('clearsession') !== null) {
         if ($this->clearSession()) {
             $this->Controller->Flash->success(__('session cleared'));
         } else {
             $this->Controller->Flash->error(__('session not cleared'));
         }
         return $this->Controller->redirect($this->_cleanedUrl('clearsession'));
     }
     // layout switch
     if ($this->Controller->request->query('layout') !== null) {
         $this->setLayout($this->Controller->request->query('layout'));
         $this->Controller->Flash->success(__('layout %s activated', $this->Controller->request->query('layout')));
         return $this->Controller->redirect($this->_cleanedUrl('layout'));
     }
     $this->issueMailing();
 }