2016-05-30 98 views
0

我在做一个cakephp项目。 我被困在这里。 我想在cakephp 3.2中获得前一个网址,但它不起作用。 这里链接是存在于电子邮件中,点击该链接后,我将重定向到登录页面,登录后,我wnato重定向到以前的网址意味着邮件中只存在的URL。 我写下面的代码来做到这一点。

$rU = $this->request->referer(); 
     if (!stristr($rU, "users/login") && !stristr($rU, "login") && !stristr($rU, "users/register") && !stristr($rU, "register") && !stristr($rU, "appadmins") && !stristr($rU, "js") && !stristr($rU, "css") && !stristr($rU, "ajax")) { 
      $this->request->session()->write('visited_page',$rU); 
     } 

Plesae suggst me。 任何建议将不胜感激。 谢谢。

回答

3

Cakephp提供了将用户重定向到他们来自的地方的功能。

AuthComponent::redirectUrl()

后登录重定向他们的redirectUrl像下面
$this->redirect($this->Auth->redirectUrl())

欲了解更多信息请访问here

1

首先在AppController.php。内部会话beforeFilter函数店前一个URL

public function beforeFilter(){ 
    $url = Router::url(NULL, true); //complete url 
    if (!preg_match('/login|logout/i', $url)){ // Restrict login and logout actions 
    $this->Session->write('prevUrl', $url); 
    } 
} 

使用,你需要重定向

if ($this->Session->read('prevUrl')){ 
$this->redirect($this->Session->read('prevUrl')); 
exit; 
} 

这是工作中的CakePHP 2.6变化beforeFilter功能的CakePHP 3作为

public function beforeFilter(\Cake\Event\Event $event){ 
    $url = Router::url(NULL, true); //complete url 
if (!preg_match('/login|logout/i', $url)){ // Restrict login and logout actions 
    $session = $this->request->session(); 
    $session->write('prevUrl', $url); 
} 
} 

用途,其中您需要重定向

$session = $this->request->session(); 
if($session->read('prevUrl')){ 
    $this->redirect($session->read('prevUrl')); 
    exit; 
} 
相关问题