php 在 Magento 中获取当前 URL 并显示一些内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25192212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Current URL in Magento and show something
提问by jehzlau
I'm trying to get the current URL in Magento and show something if I'm currently on that page. So far, this is what I did and it worked.
我正在尝试获取 Magento 中的当前 URL,如果我当前在该页面上,则显示一些内容。到目前为止,这就是我所做的并且有效。
<?php
$currentUrl = $this->helper('core/url')->getCurrentUrl();
?>
<?php if($currentUrl === 'http://powerplantv2.jehzlau.net/blog') { ?>I am in the blog page<?php } ?>
However, I don't want to hard code the URL in the source code, because If I transfer to another server, I need to modify the phtml file again.
但是,我不想在源代码中硬编码URL,因为如果我转移到另一个服务器,我需要再次修改phtml文件。
I tried everything that I found online, but it didn't work. I hope some Magento expert here can enlighted me of what I'm doing wrong. :(
我尝试了我在网上找到的所有内容,但没有奏效。我希望这里的一些 Magento 专家可以让我明白我做错了什么。:(
回答by Axel
You can retrieve the current URL path by doing the following:
您可以通过执行以下操作来检索当前 URL 路径:
$currentUrl = Mage::helper('core/url')->getCurrentUrl();
$url = Mage::getSingleton('core/url')->parseUrl($currentUrl);
$path = $url->getPath();
Then using some basic logic, you can target the /blog
page.
然后使用一些基本逻辑,您可以定位/blog
页面。
$blogPaths = array('/blog', '/blog/', '/index.php/blog/');
if(in_array($path, $blogPaths))
{
//Do something on /blog
}
回答by espradley
An alternate solution, would be to check the controller that's being called. Check the output of these and see if it works for ya. This works inside the template files.
另一种解决方案是检查正在调用的控制器。检查这些输出,看看它是否适合你。这适用于模板文件。
/**
* 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();
回答by Pankaj Upadhyay
$currentUrl = Mage::helper('core/url')->getCurrentUrl();