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
Пример #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);
 }
Пример #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);
     }
 }
Пример #4
0
 /**
  * Test that the referer is not absolute if it is '/'.
  *
  * This avoids the base path being applied twice on string urls.
  *
  * @return void
  */
 public function testRefererSlash()
 {
     $request = $this->getMockBuilder('Cake\\Network\\Request')->setMethods(['referer'])->getMock();
     $request->base = '/base';
     Router::pushRequest($request);
     $request->expects($this->any())->method('referer')->will($this->returnValue('/'));
     $controller = new Controller($request);
     $result = $controller->referer('/', true);
     $this->assertEquals('/', $result);
     $controller = new Controller($request);
     $result = $controller->referer('/some/path', true);
     $this->assertEquals('/base/some/path', $result);
 }
Пример #5
0
 /**
  * testReferer method
  *
  * @return void
  */
 public function testReferer()
 {
     $request = $this->getMock('Cake\\Network\\Request', ['referer']);
     $request->expects($this->any())->method('referer')->with(true)->will($this->returnValue('/posts/index'));
     $Controller = new Controller($request);
     $result = $Controller->referer(null, true);
     $this->assertEquals('/posts/index', $result);
     $request = $this->getMock('Cake\\Network\\Request', ['referer']);
     $request->expects($this->any())->method('referer')->with(true)->will($this->returnValue('/posts/index'));
     $Controller = new Controller($request);
     $result = $Controller->referer(array('controller' => 'posts', 'action' => 'index'), true);
     $this->assertEquals('/posts/index', $result);
     $request = $this->getMock('Cake\\Network\\Request', ['referer']);
     $request->expects($this->any())->method('referer')->with(false)->will($this->returnValue('http://localhost/posts/index'));
     $Controller = new Controller($request);
     $result = $Controller->referer();
     $this->assertEquals('http://localhost/posts/index', $result);
     $Controller = new Controller(null);
     $result = $Controller->referer();
     $this->assertEquals('/', $result);
 }
Пример #6
-1
 /**
  * SetupComponent::log404()
  *
  * @param bool $notifyAdminOnInternalErrors
  * @return void
  */
 public function log404($notifyAdminOnInternalErrors = false)
 {
     if ($this->Controller->name === 'CakeError') {
         $referer = $this->Controller->referer();
         $this->Controller->log('REF: ' . $referer . ' - URL: ' . $this->Controller->request->url, '404');
     }
 }