10

I have looked all over the internet, but can not find the following answer:

We want to get the part in Magento in .phtml after the main url so if:

domain.com/shoes.html we want to get only shoes.html

This is important because we have a multishop in .nl and .be and we want to use the hreflang. We need to give in both shops the url of both shops so as well from the .be and the .nl and then with the subcategory or product url.

In .phtml you can get the current url with: helper('core/url')->getCurrentUrl();?>

With that code you will get the total url so domain.com/shoes.html and we only want the part shoes.html

Would be great if someone knows the answer

4

5 回答 5

25

你可以这样做:

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

$path 将包含不包括域名的 URL。

于 2013-04-25T10:44:19.357 回答
8

您可以使用 PHP 的 basename 方法获取该文件名

$url = Mage::helper('core/url')->getCurrentUrl(); //http://domain.com/shoes.html
echo basename($url); //outputs shoes.html
于 2013-04-24T16:24:28.927 回答
4

我对这里的一些建议有两个问题,首先 getBaseUrl() 包含端口号,其次我的基本 URL 包含一个商店目录,例如 www.example.com/store (包含在 getBaseUrl() 和 RightThing 中) . 答案是查看 getCurrentUrl() 的代码并创建我自己的当前 URL,该 URL 没有隐藏端口。

以下代码对我有用:

<?php
$request = Mage::app()->getRequest();
$port = ':' . $request->getServer('SERVER_PORT');

$currentUrl = $request->getScheme() . '://' . $request->getHttpHost() . $port . $request->getServer('REQUEST_URI');

$relUrl = str_replace(Mage::getBaseUrl(), '', $currentUrl);
    echo '<p>Base: ' .  Mage::getBaseUrl() . '</p>';
    echo '<p>This: ' . $currentUrl . '</p>';
    echo '<p>Repl: ' . $relUrl . '</p>';
?>
于 2014-06-16T11:07:25.557 回答
1

有很多方法可以得到你想要的,这完全取决于你想要什么。您可以在执行操作时获取当前 url,并Mage::getBaseUrl()使用str_replace.

或者,您可以使用$_SERVER['REQUEST_URI']

于 2013-04-24T16:28:41.123 回答
1
$currentUrl = Mage::helper('core/url')->getCurrentUrl()

或者

$currentUrl = Mage::getUrl('*/*/*', array('_current' => true));

上面的代码可能并不总是按预期工作。查找当前 url 的更好方法是使用以下代码:

if (!in_array(Mage::app()->getFrontController()->getAction()->getFullActionName(), array('cms_index_noRoute', 'cms_index_defaultNoRoute'))) {
    $currentUrl = Mage::helper('core/url')->getCurrentUrl();
}
于 2013-08-13T08:27:24.447 回答