获取Magento中的当前URL并显示一些内容

时间:2014-08-07 21:07:23

标签: php magento

我试图在Magento中获取当前网址,并在我当前在该网页上显示内容。到目前为止,这就是我所做的并且有效。

 <?php
 $currentUrl = $this->helper('core/url')->getCurrentUrl();
 ?>     

 <?php if($currentUrl === 'http://powerplantv2.jehzlau.net/blog') { ?>I am in the blog page<?php } ?>

但是,我不想在源代码中硬编码URL,因为如果我转移到另一台服务器,我需要再次修改phtml文件。

我尝试了我在网上找到的所有东西,但它没有用。我希望这里的一些Magento专家能够让我知道我做错了什么。 :(

3 个答案:

答案 0 :(得分:73)

您可以通过执行以下操作来检索当前的URL路径:

$currentUrl = Mage::helper('core/url')->getCurrentUrl();
$url = Mage::getSingleton('core/url')->parseUrl($currentUrl);
$path = $url->getPath();

然后使用一些基本逻辑,您可以定位/blog页面。

$blogPaths = array('/blog', '/blog/', '/index.php/blog/');
if(in_array($path, $blogPaths))
{
    //Do something on /blog
}

答案 1 :(得分:5)

另一种解决方案是检查被调用的控制器。检查这些输出,看看它是否适用于你。这适用于模板文件。

select e.name, em.width, emr.height
from
  public.entry e
left join
  public.entry_metadata em
on
  em.entry_id = e.id and em.variable = 'width'
left join
  public.entry_metadata emr
on
  emr.entry_id = e.id and emr.variable = 'height'

答案 2 :(得分:3)

$currentUrl = Mage::helper('core/url')->getCurrentUrl();