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

2022-08-30 15:03:57

我正在尝试在Magento中获取当前URL,如果我当前在该页面上,则显示一些内容。到目前为止,这就是我所做的,它奏效了。

 <?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专家可以让我了解我做错了什么。:(


答案 1

您可以通过执行以下操作来检索当前 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
}

答案 2

另一种解决方案是检查正在调用的控制器。检查这些的输出,看看它是否适用于ya。这在模板文件中起作用。

 /**
 * get Controller name
 */
$this->getRequest()->getControllerName();

/**
 * get Action name, i.e. the function inside the controller
 */
$this->getRequest()->getActionName();

/**
 * get Router name
 */
$this->getRequest()->getRouteName();

/**
 * get module name
 */
$this->getRequest()->getModuleName();

推荐