php新手寻找关于改进Magento代码的建议
我正在编写代码,将动态添加到Magento商店中每个网页的php新手寻找关于改进Magento代码的建议,php,magento,refactoring,helper,Php,Magento,Refactoring,Helper,我正在编写代码,将动态添加到Magento商店中每个网页的部分 我在Magento中编写了一些助手函数,我从html/head.phtml模板文件中调用了这些函数。我使用它们为每个Magento商店视图获取相应的url。他们工作得很好,但我觉得代码可能会更好 例如: 我有一些在每个函数中重用的变量,我可以将它们定义为类全局变量,并根据需要在每个函数中调用它们。我原以为全局关键字可以实现这一点,但在Magento中似乎不起作用。e、 g.$englishItalian变量 有没有其他方法可以重构h
部分
我在Magento中编写了一些助手函数,我从html/head.phtml模板文件中调用了这些函数。我使用它们为每个Magento商店视图获取相应的url。他们工作得很好,但我觉得代码可能会更好
例如:
$englishItalian
变量<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" />
将重复的阵列向上移动到顶部
Namespace/Utility/Helper/Data.php Helper类
<?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
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;
}
}
您可以将$englishItalian
、$englishFrench
和$italianFrench
从函数中拉出。在受保护的$englishItalian=…
顶部声明一次。然后,在函数内部,当您引用它时,您将使用$this->englishItalian
@slapyo,它给出了一个致命错误:无法访问第128行的…/Utility/Helper/Data.php中的空属性
谢谢,效果更好。当我在你的评论后尝试它时,我肯定有一些小的语法错误