Obtener URL actual en Magento y mostrar algo


Estoy tratando de obtener la URL actual en Magento y mostrar algo si estoy actualmente en esa página. Hasta ahora, esto es lo que hice y funcionó.

 <?php
 $currentUrl = $this->helper('core/url')->getCurrentUrl();
 ?>     

 <?php if($currentUrl === 'http://powerplantv2.jehzlau.net/blog') { ?>I am in the blog page<?php } ?>

Sin embargo, no quiero codificar la URL en el código fuente, porque Si transfiero a otro servidor, necesito modificar el archivo phtml nuevamente.

Probé todo lo que encontré en línea, pero no funcionó. Espero que algún experto de Magento aquí pueda iluminarme de lo que estoy haciendo mal. :(

 26
Author: jehzlau, 2014-08-08

3 answers

Puede recuperar la ruta de URL actual haciendo lo siguiente:

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

Luego, usando alguna lógica básica, puede dirigirse a la página /blog.

$blogPaths = array('/blog', '/blog/', '/index.php/blog/');
if(in_array($path, $blogPaths))
{
    //Do something on /blog
}
 69
Author: Axel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-08-07 23:21:52

Una solución alternativa sería comprobar el controlador al que se está llamando. Compruebe la salida de estos y ver si funciona para ti. Esto funciona dentro de los archivos de plantilla.

 /**
 * 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
Author: espradley,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-28 02:41:12
$currentUrl = Mage::helper('core/url')->getCurrentUrl();
 2
Author: Pankaj Upadhyay,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-08 07:03:25