【问题标题】:How to set external URL link to Magento admin menu如何设置外部 URL 链接到 Magento 管理菜单
【发布时间】:2015-04-03 11:55:29
【问题描述】:

我正在开发一个模块,我在 Magento 管理员中使用 adminhtml.xml 创建了一个菜单。

现在我想将其中一个菜单链接到外部 URL 并设置 target="blank"。但我不确定如何在adminhtml.xml 中做到这一点。这是我的代码。

<?xml version="1.0"?>
<config>
    <menu>
        <system>
            <children>
                <convert translate="title">
                    <children>
                        <importmagmi translate="title" module="importexport">
                            <title>MagMi Importer</title>
                            <action><url helper="https://externalurl.com"/></action>
                            <sort_order>100</sort_order>
                        </importmagmi>
                    </children>
                </convert>
            </children>
        </system>
    </menu>
</config>

当我检查它在外部 url 之前添加当前域名时。例如:http://mydomainname.com/https://externalurl.com

我想知道如何只设置外部 URL?

【问题讨论】:

    标签: php xml magento magento-1.7


    【解决方案1】:

    在&lt;action&gt; 标签内,您可以将module/controller/action 放在您的模块中。

    然后创建这个动作并输入如下内容:

    public function locationAction()
    {
        $this->_redirectUrl('http://www.example.com/');
    }
    

    有关 Magento 控制器操作中的标准重定向实现,请参阅 Mage_Core_Controller_Varien_Action::_redirectUrl。

    【讨论】:

      【解决方案2】:

      不幸的是,这不可能开箱即用。为此,您必须覆盖 Mage_Adminhtml_Block_Page_Menu 类。

      我建议修改 _buildMenuArray 方法以支持 adminhtml.xml 中的“external_url”配置选项,就像这样

      if( $child->external_url ) {
          $menuArr['url'] = (string)$child->external_url;
          $menuArr['is_external'] = true;
      } 
      elseif ($child->action) {
          $menuArr['url'] = $this->_url->getUrl((string)$child->action, array('_cache_secret_key' => true));
      } else {
          $menuArr['url'] = '#';
          $menuArr['click'] = 'return false';
      }
      

      和getMenuLevel方法分别

      $html .= '<li ' . (!empty($item['children']) ? 'onmouseover="Element.addClassName(this,\'over\')" '
                  . 'onmouseout="Element.removeClassName(this,\'over\')"' : '') . ' class="'
                  . (!$level && !empty($item['active']) ? ' active' : '') . ' '
                  . (!empty($item['children']) ? ' parent' : '')
                  . (!empty($level) && !empty($item['last']) ? ' last' : '')
                  . ' level' . $level . '"> <a ' . ($item['is_external'] ? 'target="_blank" ' : '') . 'href="' . $item['url'] . '" '
                  . (!empty($item['title']) ? 'title="' . $item['title'] . '"' : '') . ' '
                  . (!empty($item['click']) ? 'onclick="' . $item['click'] . '"' : '') . ' class="'
                  . ($level === 0 && !empty($item['active']) ? 'active' : '') . '"><span>'
                  . $this->escapeHtml($item['label']) . '</span></a>' . PHP_EOL;
      

      然后你可以添加到你的配置中

      <?xml version="1.0"?>
      <config>
          <menu>
              <system>
                  <children>
                      <convert translate="title">
                          <children>
                              <importmagmi translate="title" module="importexport">
                                  <title>MagMi Importer</title>
                                  <external_url>https://externalurl.com</external_url>                                <sort_order>100</sort_order>
                              </importmagmi>
                          </children>
                      </convert>
                  </children>
              </system>
          </menu>
      </config>
      

      记得重写类,不要修改核心类。

      【讨论】:

      • 尽可能防止类覆盖。
      • 你当然是对的,应该尽可能防止类重写。但在这种情况下,这是不可能的,因为这是按照原始问题的要求在新窗口 (target = '_blank') 中打开链接的唯一方法。
      【解决方案3】:
      <?php
      $url = 'http://example.com';
      $this->_redirectUrl('http://example.com');
      
      Mage::app()->getResponse()->setRedirect($url)->sendResponse();
      
      Mage::app()->getFrontController()->getResponse()->setRedirect($url)->sendResponse();
      ?>
      

      【讨论】:

      • 您的答案有什么介绍,可能是?只是发布代码并不提供信息
      猜你喜欢
      • 2013-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多