Cake\Controller\Controller::redirect PHP Метод

redirect() публичный Метод

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)
Результат Cake\Network\Response | null
    public function redirect($url, $status = 302)
    {
        $this->autoRender = false;
        $response = $this->response;
        if ($status) {
            $response->statusCode($status);
        }
        $event = $this->dispatchEvent('Controller.beforeRedirect', [$url, $response]);
        if ($event->result instanceof Response) {
            return $event->result;
        }
        if ($event->isStopped()) {
            return null;
        }
        if (!$response->location()) {
            $response->location(Router::url($url, true));
        }
        return $response;
    }

Usage Example

Пример #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);
 }
All Usage Examples Of Cake\Controller\Controller::redirect