web-dev-qa-db-ja.com

Magentoで現在のURLを取得して何かを表示する

現在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の専​​門家が、私が間違っていることを教えてくれることを願っています。 :(

28
jehzlau

以下を実行することにより、現在の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
}
73
Axel

別の解決策は、呼び出されているコントローラーを確認することです。これらの出力を確認し、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();
5
espradley
$currentUrl = Mage::helper('core/url')->getCurrentUrl();
4
Pankaj Upadhyay