【问题标题】:$this->request->referer() is not working in cakephp 3.2 from email link$this->request->referer() 在 cakephp 3.2 中无法通过电子邮件链接工作
【发布时间】:2016-05-30 08:09:18
【问题描述】:

我正在做一个 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);
        }

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

【问题讨论】:

    标签: cakephp cakephp-3.0 cakephp-3.2


    【解决方案1】:

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

    它的AuthComponent::redirectUrl()

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

    欲了解更多信息,请访问here

    【讨论】:

      【解决方案2】:

      首先在 AppController.php 中。在 beforeFilter 函数内部,将以前的 url 存储在 session 中

      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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-14
        • 2011-03-15
        • 2016-04-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多