我必须修改网址才能更改语言

时间:2018-05-09 13:25:54

标签: php

在我的应用程序中,我遇到了生成url的问题我会知道你是否有意解决这个问题。

谢谢

普通网址

当我更改语言时,我对网址没有任何问题

http://localhost/test/boutique/index.php
http://localhost/test/boutique/index.php?language=fr
http://localhost/test/boutique/index.php?language=en

我在索引页面中选择了一种语言并继续使用该产品,没问题

http://localhost/test/boutique/index.php?Products&Description&products_id=1

但是,如果我在产品中并且我更改了语言,则问题会显示在url中,而且=&

localhost/test/boutique/index.php?Products=&Description=&products_id=1&language=en

我认为产生了url问题,但导航中没有发生。只有当我更改页面内的语言时才会出现问题

有关于该链接的代码。

  public static function link($page, $parameters = null, $add_session_id = true, $search_engine_safe = true)  {

      $page = HTML::sanitize($page);

      $site = $req_site = static::$site;

      if ((strpos($page, '/') !== false) && (preg_match('/^([A-Z][A-Za-z0-9-_]*)\/(.*)$/', $page, $matches) === 1) && OSCOM::siteExists($matches[1], false)) {
          $req_site = $matches[1];
          $page = $matches[2];
      }

      if (!is_bool($add_session_id)) {
        $add_session_id = true;
      }

      if (!is_bool($search_engine_safe)) {
        $search_engine_safe = true;
      }

      if (($add_session_id === true) && ($site !== $req_site)) {
        $add_session_id = false;
      }

      $link = static::getConfig('http_server', $req_site) . static::getConfig('http_path', $req_site) . $page;

      if (!empty($parameters)) {
        $p = HTML::sanitize($parameters);

         $p = str_replace([
                          "\\", // apps
                          '{', // product attributes
                          '}' // product attributes
                          ], [
                            '%5C',
                            '%7B',
                            '%7D'
                          ], $p);


        $link .= '?' . $p;
        $separator = '&';
      } else {
        $separator = '?';
      }

      while((substr($link, -1) == '&') || (substr($link, -1) == '?')) {
        $link = substr($link, 0, -1);
      }

// Add the session ID when moving from different HTTP and HTTPS servers, or when SID is defined
      if (($add_session_id === true) && Registry::exists('Session')) {
        $OSCOM_Session = Registry::get('Session');

        if ($OSCOM_Session->hasStarted() && ($OSCOM_Session->isForceCookies() === false)) {
          if ((strlen(SID) > 0) || (((HTTP::getRequestType() == 'NONSSL') && (parse_url(static::getConfig('http_server', $req_site), PHP_URL_SCHEME) == 'https')) || ((HTTP::getRequestType() == 'SSL') && (parse_url(static::getConfig('http_server', $req_site), PHP_URL_SCHEME) == 'http')))) {
            $link .= $separator . HTML::sanitize(session_name() . '=' . session_id());
          }
        }
      }

      while(strpos($link, '&&') !== false) {
        $link = str_replace('&&', '&', $link);
      }

      if ($search_engine_safe === true && defined('SEARCH_ENGINE_FRIENDLY_URLS') && (SEARCH_ENGINE_FRIENDLY_URLS == 'true' && SEFU::start()) && static::getSite() != 'ClicShoppingAdmin') {
//        $link = str_replace(['?', '&', '='], '/', $link);
        $link = str_replace(['?', '&', '='], ['/', '/', ','], $link);
      }

      return $link;
    }


   public static function link($url, $element, $parameters = null) {
      return '<a href="' . $url . '"' . (!empty($parameters) ? ' ' . $parameters : '') . '>' . $element . '</a>';
    }






public function getFlag() {
      if (!isset($_GET['Checkout'])) {
        $content = '';

        $get_params = [];

        foreach ( $_GET as $key => $value ) {
          if (($key != 'language') && ($key != Registry::get('Session')->getName()) && ($key != 'x') && ($key != 'y')) {
            $get_params[] = $key . '=' . $value;
          }
        }

        $get_params = implode($get_params, '&');


        if ( !empty($get_params) ) {
          $get_params .= '&';
        }

        foreach ($this->getAll() as $value) {
          $content .= ' ' . HTML::link(OSCOM::link('index.php', $get_params . 'language=' . $value['code']), $this->getImage($value['code'])) . ' ';
        }
      }

      return $content;
    }

$_GET值:getFlag函数

  'Products' => string '' (length=0)
  'Description' => string '' (length=0)
  'products_id' => string '1' (length=1)
  'language' => string 'en' (length=2)

1 个答案:

答案 0 :(得分:2)

你的问题似乎在这里:

$get_params[] = $key . '=' . $value;

也许试试:

$get_params[] = ($value) ? "$key=$value" : $key;

(如果值为空,则不附加等号和值,只附加键。)

你应该阅读@Patrick Q的评论。