2014-11-15 59 views
0

我正在爲我們的Magento商店中的每個網頁的<head>部分添加動態hreflang elements代碼。php新手正在尋找關於改善Magento中的代碼的建議

我已經在Magento中編寫了一些幫助函數,我從我們的html/head.phtml模板文件中調用它。我使用它們爲每個Magento商店視圖獲取相應的URL。他們工作正常,但我覺得代碼可能會更好。

例如:

  1. 我有一些變數,我在每個功能重用我可以將它們定義爲類全局變量,並根據需要每個函數中調用它們。我認爲全球的關鍵詞可以達到這個目標,但它似乎並不適用於Magento。例如變量爲$englishItalian
  2. 是否有任何其他方式可以重構我的助手類/ data.php中的代碼?
  3. 我應該將代碼從助手類移動到Magento塊嗎?我如何從模板中調用這個?
  4. 無論如何,在返回之前檢查URL是否存在?

所有建議歡迎

HTML/head.phtml模板

<link rel="alternate" href="<?php echo Mage::helper('utility')->getEnglishDefaultUrl(); ?>" hreflang="en" /> 
<link rel="alternate" href="<?php echo Mage::helper('utility')->getEnglishUsUrl(); ?>" hreflang="en-us" /> 
<link rel="alternate" href="<?php echo Mage::helper('utility')->getItalianItalyUrl() ?>" hreflang="it-it" /> 
<link rel="alternate" href="<?php echo Mage::helper('utility')->getEnglishCanadaUrl(); ?>" hreflang="en-ca" /> 
<link rel="alternate" href="<?php echo Mage::helper('utility')->getFrenchCanadaUrl(); ?>" hreflang="fr-ca" /> 

命名空間/工具/助手/ Data.php助手類

<?php 

class Namespace_Utility_Helper_Data extends Mage_Core_Helper_Abstract 
{ 
    public function getEnglishDefaultUrl() 
    { 
     $curStoreId = Mage::app()->getStore()->getStoreId(); // get current store view ID 
     $href = Mage::getUrl('', array('_current' => true,'_use_rewrite' => true,'_store' => 1,)); // get url for particular tore view 
     $href = $this->cleanUrl($href); // remove query strings from url 
     if ($curStoreId == 4) { // translate href depending on current store view ID 
      $href = $this->transItalianToEnglish($href); 
      return $href; 
     } elseif($curStoreId == 6){ 
      $href = $this->transFrenchToEnglish($href); 
      return $href; 
     } else { 
      return $href; 
     } 
    } 

    public function getEnglishUsUrl() 
    { 
     $curStoreId = Mage::app()->getStore()->getStoreId(); 
     $href = Mage::getUrl('', array('_current' => true,'_use_rewrite' => true,'_store' => 2,)); 
     $href = $this->cleanUrl($href); 
     if ($curStoreId == 4) { 
      $href = $this->transItalianToEnglish($href); 
      return $href; 
     } elseif($curStoreId == 6){ 
      $href = $this->transFrenchToEnglish($href); 
      return $href; 
     } else { 
      return $href; 
     } 
    } 

    public function getItalianItalyUrl() 
    { 
     $curStoreId = Mage::app()->getStore()->getStoreId(); 
     $href = Mage::getUrl('', array('_current' => true,'_use_rewrite' => true,'_store' => 4,)); 
     $href = $this->cleanUrl($href); 
     if ($curStoreId == 1 || $curStoreId == 2 || $curStoreId == 5) { 
      $href = $this->transEnglishToItalian($href); 
      return $href; 
     }elseif ($curStoreId == 6){ 
      $href = $this->transFrenchToItalian($href); 
      return $href; 
     }else { 
      return $href; 
     } 
    } 

    public function getEnglishCanadaUrl() 
    { 
     $curStoreId = Mage::app()->getStore()->getStoreId(); 
     $href = Mage::getUrl('', array('_current' => true,'_use_rewrite' => true,'_store' => 5,)); 
     $href = $this->cleanUrl($href); 
     if ($curStoreId == 4) { 
      $href = $this->transItalianToEnglish($href); 
      return $href; 
     } elseif($curStoreId == 6){ 
      $href = $this->transFrenchToEnglish($href); 
      return $href; 
     } else { 
      return $href; 
     } 
    } 

    public function getFrenchCanadaUrl() 
    { 
     $curStoreId = Mage::app()->getStore()->getStoreId(); 
     $href = Mage::getUrl('', array('_current' => true,'_use_rewrite' => true,'_store' => 6,)); 
     $href = $this->cleanUrl($href); 
     if ($curStoreId == 1 || $curStoreId == 2 || $curStoreId == 5) { 
      $href = $this->transEnglishToFrench($href); 
      return $href; 
     }elseif ($curStoreId == 4){ 
      $href = $this->transItalianToFrench($href); 
      return $href; 
     }else { 
      return $href; 
     } 
    } 

    public function cleanUrl($url) // remove query string from href 
    { 
     $url = preg_replace('/\?.*/', '', $url); 
     return $url; 
    } 

    public function transEnglishToItalian($url) 
    { 
     $englishItalian = array( // use an associative array to store translations for urls 
      "products"=>"prodotti", 
      "science"=>"scienza", 
      "terms-and-conditions"=>"termini-e-condizioni", 
      "shipping"=>"spedizione", 
     ); 
     foreach ($englishItalian as $key => $value) { // iterate over the array 
      if (strpos($url,$key) !== false) { // check if url has english translation word 
       $url = str_replace($key, $value, $url); // replace the english work with the Italian word 
      } 
     } 
     return $url; 
    } 

    public function transItalianToEnglish($url) 
    { 
     $englishItalian = array(
      "products"=>"prodotti", 
      "science"=>"scienza", 
      "terms-and-conditions"=>"termini-e-condizioni", 
      "shipping"=>"spedizione", 
     ); 
     foreach ($englishItalian as $key => $value) { 
      if (strpos($url,$value) !== false) { 
       $url = str_replace($value, $key, $url); 
      } 
     } 
     return $url; 
    } 

    public function transEnglishToFrench($url) 
    { 
     $englishFrench = array(
      "products"=>"produits", 
      "shipping"=>"livraison", 
     ); 
     foreach ($englishFrench as $key => $value) { 
      if (strpos($url,$key) !== false) { 
       $url = str_replace($key, $value, $url); 
      } 
     } 
     return $url; 
    } 
    public function transFrenchToEnglish($url) 
    { 
     $englishFrench = array(
      "products"=>"produits", 
      "shipping"=>"livraison", 
     ); 
     foreach ($englishFrench as $key => $value) { 
      if (strpos($url,$value) !== false) { 
       $url = str_replace($value, $key, $url); 
      } 
     } 
     return $url; 
    } 

    public function transItalianToFrench($url) 
    { 
     $italianFrench = array(
      "prodotti"=>"produits", 
      "scienza"=>"science", 
      "termini-e-condizioni"=>"terms-and-conditions", 
      "spedizione"=>"livraison", 
     ); 
     foreach ($italianFrench as $key => $value) { 
      if (strpos($url,$key) !== false) { 
       $url = str_replace($key, $value, $url); 
      } 
     } 
     return $url; 
    } 

    public function transFrenchToItalian($url) 
    { 
     $italianFrench = array(
      "prodotti"=>"produits", 
      "scienza"=>"science", 
      "termini-e-condizioni"=>"terms-and-conditions", 
      "spedizione"=>"livraison", 
     ); 
     foreach ($italianFrench as $key => $value) { 
      if (strpos($url,$value) !== false) { 
       $url = str_replace($value, $key, $url); 
      } 
     } 
     return $url; 
    } 
} 
+0

你可以拉'$ englishItalian','$ englishFrench'和'$ italianFrench'出來的功能。聲明他們曾經頂級'保護$英語意大利= ...'。然後在函數內部引用它時,將使用'$ this-> englishItalian'。 – slapyo 2014-11-15 00:12:26

+0

@slapyo給了'致命錯誤:無法訪問128行上的.../Utility/Helper/Data.php中的空屬性' – Holly 2014-11-15 00:34:44

回答

1

移動你重複到頂部的數組。

命名空間/工具/助手/ Data.php助手類

<?php 

class Namespace_Utility_Helper_Data extends Mage_Core_Helper_Abstract 
{ 
    protected $englishItalian = array(
     "products"=>"prodotti", 
     "science"=>"scienza", 
     "terms-and-conditions"=>"termini-e-condizioni", 
     "shipping"=>"spedizione", 
    ); 

    protected $englishFrench = array(
     "products"=>"produits", 
     "shipping"=>"livraison", 
    ); 

    protected $italianFrench = array(
     "prodotti"=>"produits", 
     "scienza"=>"science", 
     "termini-e-condizioni"=>"terms-and-conditions", 
     "spedizione"=>"livraison", 
    ); 

    public function getEnglishDefaultUrl() 
    { 
     $curStoreId = Mage::app()->getStore()->getStoreId(); // get current store view ID 
     $href = Mage::getUrl('', array('_current' => true,'_use_rewrite' => true,'_store' => 1,)); // get url for particular tore view 
     $href = $this->cleanUrl($href); // remove query strings from url 
     if ($curStoreId == 4) { // translate href depending on current store view ID 
      $href = $this->transItalianToEnglish($href); 
      return $href; 
     } elseif($curStoreId == 6){ 
      $href = $this->transFrenchToEnglish($href); 
      return $href; 
     } else { 
      return $href; 
     } 
    } 

    public function getEnglishUsUrl() 
    { 
     $curStoreId = Mage::app()->getStore()->getStoreId(); 
     $href = Mage::getUrl('', array('_current' => true,'_use_rewrite' => true,'_store' => 2,)); 
     $href = $this->cleanUrl($href); 
     if ($curStoreId == 4) { 
      $href = $this->transItalianToEnglish($href); 
      return $href; 
     } elseif($curStoreId == 6){ 
      $href = $this->transFrenchToEnglish($href); 
      return $href; 
     } else { 
      return $href; 
     } 
    } 

    public function getItalianItalyUrl() 
    { 
     $curStoreId = Mage::app()->getStore()->getStoreId(); 
     $href = Mage::getUrl('', array('_current' => true,'_use_rewrite' => true,'_store' => 4,)); 
     $href = $this->cleanUrl($href); 
     if ($curStoreId == 1 || $curStoreId == 2 || $curStoreId == 5) { 
      $href = $this->transEnglishToItalian($href); 
      return $href; 
     }elseif ($curStoreId == 6){ 
      $href = $this->transFrenchToItalian($href); 
      return $href; 
     }else { 
      return $href; 
     } 
    } 

    public function getEnglishCanadaUrl() 
    { 
     $curStoreId = Mage::app()->getStore()->getStoreId(); 
     $href = Mage::getUrl('', array('_current' => true,'_use_rewrite' => true,'_store' => 5,)); 
     $href = $this->cleanUrl($href); 
     if ($curStoreId == 4) { 
      $href = $this->transItalianToEnglish($href); 
      return $href; 
     } elseif($curStoreId == 6){ 
      $href = $this->transFrenchToEnglish($href); 
      return $href; 
     } else { 
      return $href; 
     } 
    } 

    public function getFrenchCanadaUrl() 
    { 
     $curStoreId = Mage::app()->getStore()->getStoreId(); 
     $href = Mage::getUrl('', array('_current' => true,'_use_rewrite' => true,'_store' => 6,)); 
     $href = $this->cleanUrl($href); 
     if ($curStoreId == 1 || $curStoreId == 2 || $curStoreId == 5) { 
      $href = $this->transEnglishToFrench($href); 
      return $href; 
     }elseif ($curStoreId == 4){ 
      $href = $this->transItalianToFrench($href); 
      return $href; 
     }else { 
      return $href; 
     } 
    } 

    public function cleanUrl($url) // remove query string from href 
    { 
     $url = preg_replace('/\?.*/', '', $url); 
     return $url; 
    } 

    public function transEnglishToItalian($url) 
    { 
     foreach ($this->englishItalian as $key => $value) { // iterate over the array 
      if (strpos($url,$key) !== false) { // check if url has english translation word 
       $url = str_replace($key, $value, $url); // replace the english work with the Italian word 
      } 
     } 
     return $url; 
    } 

    public function transItalianToEnglish($url) 
    { 
     foreach ($this->englishItalian as $key => $value) { 
      if (strpos($url,$value) !== false) { 
       $url = str_replace($value, $key, $url); 
      } 
     } 
     return $url; 
    } 

    public function transEnglishToFrench($url) 
    { 

     foreach ($this->englishFrench as $key => $value) { 
      if (strpos($url,$key) !== false) { 
       $url = str_replace($key, $value, $url); 
      } 
     } 
     return $url; 
    } 
    public function transFrenchToEnglish($url) 
    { 
     foreach ($this->englishFrench as $key => $value) { 
      if (strpos($url,$value) !== false) { 
       $url = str_replace($value, $key, $url); 
      } 
     } 
     return $url; 
    } 

    public function transItalianToFrench($url) 
    { 
     foreach ($this->italianFrench as $key => $value) { 
      if (strpos($url,$key) !== false) { 
       $url = str_replace($key, $value, $url); 
      } 
     } 
     return $url; 
    } 

    public function transFrenchToItalian($url) 
    { 
     foreach ($this->italianFrench as $key => $value) { 
      if (strpos($url,$value) !== false) { 
       $url = str_replace($value, $key, $url); 
      } 
     } 
     return $url; 
    } 
} 
+0

謝謝你的工作要好得多。當我在評論後嘗試它時,我必須有一些小的語法錯誤 – Holly 2014-11-16 11:25:59