我正在尝试获取Magento中的当前URL并显示一些内容,如果我当前正在该页面上.到目前为止,这就是我所做的并且有效.
<?php
$currentUrl = $this->helper('core/url')->getCurrentUrl();
?>
<?php if($currentUrl === 'http://powerplantv2.jehzlau.net/blog') { ?>I am in the blog page<?php } ?>
Run Code Online (Sandbox Code Playgroud)
但是,我不想在源代码中硬编码URL,因为如果我转移到另一台服务器,我需要再次修改phtml文件.
我尝试了我在网上找到的所有内容,但它没有用.我希望这里的一些Magento专家能够让我知道我做错了什么.:(
Axe*_*xel 73
您可以通过执行以下操作来检索当前URL路径:
$currentUrl = Mage::helper('core/url')->getCurrentUrl();
$url = Mage::getSingleton('core/url')->parseUrl($currentUrl);
$path = $url->getPath();
Run Code Online (Sandbox Code Playgroud)
然后使用一些基本逻辑,您可以定位/blog页面.
$blogPaths = array('/blog', '/blog/', '/index.php/blog/');
if(in_array($path, $blogPaths))
{
//Do something on /blog
}
Run Code Online (Sandbox Code Playgroud)
另一种解决方案是检查被调用的控制器.检查这些输出,看看它是否适用于你.这适用于模板文件.
/**
* 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();
Run Code Online (Sandbox Code Playgroud)
$currentUrl = Mage::helper('core/url')->getCurrentUrl();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
90978 次 |
| 最近记录: |