0

我正在做一个 cakephp 项目。我被困在这里。我想在 cakephp 3.2 中获取以前的 url,但它不起作用。此处的链接存在于电子邮件中,单击该链接后,我将重定向到登录页面,登录后,我想重定向到之前的 url 意味着该 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);
        }

请给我建议。任何建议将不胜感激。谢谢你。

4

2 回答 2

3

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

它的AuthComponent::redirectUrl()

登录后将它们重定向到如下所示的redirectUrl
$this->redirect($this->Auth->redirectUrl())

欲了解更多信息,请访问这里

于 2016-05-30T09:42:01.657 回答
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 中工作,将 cakephp 3 中的 beforeFilter 函数更改为

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;
}
于 2016-05-31T04:45:29.473 回答