php Magento 在主 url 之后获取 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16197003/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:41:47  来源:igfitidea点击:

Magento Get url after the main url

magentophp

提问by Coen

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:

我们希望在主 url 之后的 .phtml 中获取 Magento 中的部分,因此如果:

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

domain.com/shoes.html 我们只想得到 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.

这很重要,因为我们在 .nl 和 .be 中有一个 multishop,我们想使用 hreflang。我们需要从 .be 和 .nl 以及子类别或产品 url 中提供两家商店的网址。

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

在 .phtml 中,您可以使用以下命令获取当前 url: 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

使用该代码,您将获得总网址,因此 domain.com/shoes.html 我们只想要部分 shoes.html

Would be great if someone knows the answer

如果有人知道答案会很棒

回答by Jaymz

You could do this:

你可以这样做:

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

$path will contain the URL excluding the domain name.

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

回答by Kalpesh

You can get that filename by using PHP's basename method

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

$url = Mage::helper('core/url')->getCurrentUrl(); //http://domain.com/shoes.html
echo basename($url); //outputs shoes.html

回答by Kevin Sadler

I had two problems with some of the suggestions here, firstly getBaseUrl() included the port number, and secondly my base URL includes a store directory, e.g. www.example.com/store (which is included in getBaseUrl() and the RightThing). The answer was to look at the code for getCurrentUrl() and create my own current URL that didn't hide the port.

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

The following code worked for me:

以下代码对我有用:

<?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>';
?>

回答by nachito

There are many ways to get what you're asking for, it just depends exactly what you want. You could get the current url as you're doing and remove the base url (Mage::getBaseUrl()) with a str_replace.

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

Or, you could use $_SERVER['REQUEST_URI']

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

回答by MagePsycho

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

or

或者

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

above code may not work always as expected. Better way to find the current url is to use the following code:

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

if (!in_array(Mage::app()->getFrontController()->getAction()->getFullActionName(), array('cms_index_noRoute', 'cms_index_defaultNoRoute'))) {
????$currentUrl = Mage::helper('core/url')->getCurrentUrl();
}