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

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

Returns the referring URL for this request.
public referer ( string | array | null $default = null, boolean $local = false ) : string
$default string | array | null Default URL to use if HTTP_REFERER cannot be read from headers
$local boolean If true, restrict referring URLs to local server
Результат string Referring URL
    public function referer($default = null, $local = false)
    {
        if (!$this->request) {
            return Router::url($default, !$local);
        }
        $referer = $this->request->referer($local);
        if ($referer === '/' && $default && $default !== $referer) {
            $url = Router::url($default, !$local);
            if ($local && $this->request->base && strpos($url, $this->request->base) === 0) {
                $url = substr($url, strlen($this->request->base));
                if ($url[0] !== '/') {
                    $url = '/' . $url;
                }
                return $url;
            }
            return $url;
        }
        return $referer;
    }

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::referer