php新手寻找改善Magento代码的建议

时间:2014-11-15 00:00:37

标签: php magento refactoring helper

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

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

例如:

  1. 我有一些变量,我在每个函数中重用,我可以将它们定义为类全局变量,并根据需要在每个函数中调用它们。我认为全球关键词会实现这一目标,但它似乎并没有在Magento中发挥作用。例如$englishItalian变量。
  2. 还有其他方法可以重构我的帮助器类/ data.php中的代码吗?
  3. 我应该将代码从帮助程序类移动到Magento块吗?我如何从模板中调用它?
  4. 无论如何还要在返回之前检查URL是否存在?
  5. 欢迎所有建议

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

1 个答案:

答案 0 :(得分: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;
    }
}