PHP新手正在寻找有关改进Magento代码的建议


php newbie looking for advice on improving code in Magento

我正在编码以将动态hreflang元素添加到Magento商店每个网页的<head>部分。

我在Magento中编写了一些帮助程序函数,我从我们的html/head.phtml模板文件中调用这些函数。 我使用它们来获取每个Magento商店视图的相应URL。它们工作正常,但我觉得代码可以更好。

例如:

    我有一些变量,我可以在每个函数中
  1. 重用它们,我可以将它们定义为类全局变量并根据需要在每个函数中调用它们。 我以为全局关键字可以实现这一点,但它似乎在 Magento 中不起作用。例如$englishItalian变量。
  2. 还有其他方法可以在我的帮助程序类/数据中重构代码吗.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" />

命名空间/实用程序/帮助程序/数据.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;
    }
}

将重复的数组移动到顶部。

命名空间/实用程序/帮助程序/数据.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;
    }
}