drh47606 2017-02-04 13:11
浏览 34
已采纳

如何在prestashop中将变量从控制器传递给控制器​​?

is there a way to pass variables from a controller to another in prestashop? I'm tring to pass the new_address variable on an AddressController override like this:

Tools::redirect('index.php?controller='.$back.($mod ? '&back='.$mod : '') . '&new_address=' . $address->id);

NOTE that this is the original line + . '&new_address=' . $address->id, so I have to stick to Tools::redirect.

By using that line, no new_address is found on the next page in $_GET. From OrderController and ParentOrderController too, I don't see it.

  • 写回答

1条回答 默认 最新

  • duanmeng7865 2017-02-04 14:28
    关注

    I've found the reason in Tools::redirect. It has a line like this:

    $url = Tools::strReplaceFirst('&', '?', $url); 
    

    here they are basically excluding any queryvar other than the first one, so you won't find any if you add some. If you have index.php?a=1&b=2, you'll get index.php?a=1?b=2. I don't really see the point... Maybe it's a bug.

    So I overridden Tools::redirect like this (modded lines are commented):

    public static function redirect($url, $base_uri = __PS_BASE_URI__, Link $link = null, $headers = null){
      if (!$link) $link = Context::getContext()->link;
    
      $querystring = array_pop(explode('?', $url)); // MOD: Save the original querystring. I take the last item in array because sometimes (i think it's a bug) the $url is like index.php?controller=order.php?step=1, so 2 question marks.
    
      if (strpos($url, 'http://') === false && strpos($url, 'https://') === false && $link) {
        if (strpos($url, $base_uri) === 0) {
          $url = substr($url, strlen($base_uri));
        }
        if (strpos($url, 'index.php?controller=') !== false && strpos($url, 'index.php/') == 0) {
          $url = substr($url, strlen('index.php?controller='));
          if (Configuration::get('PS_REWRITING_SETTINGS')) {
            $url = Tools::strReplaceFirst('&', '?', $url); // ...Don't see the point here...
          }
        }
    
        $explode = explode('?', $url);
    
        // don't use ssl if url is home page
        // used when logout for example
        $use_ssl = !empty($url);
        $url = $link->getPageLink($explode[0], $use_ssl);
    
        if($querystring) $url .= '?'.$querystring; // MOD: adding full querystring!! Also deleted 3 lines that added $explode[1] instead
      }
    
      // Send additional headers
      if ($headers) {
        if (!is_array($headers)) $headers = array($headers);
    
        foreach ($headers as $header) {
          header($header);
        }
      }
    
      header('Location: '.$url);
      exit;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog