【问题标题】:URL Rewrites for magento custom modulemagento 自定义模块的 URL 重写
【发布时间】:2012-08-29 12:20:15
【问题描述】:

我正在尝试为我的模块创建一些用户友好的 URL。

模块名称正在登陆。现在,我使用索引控制器和索引操作,然后从 URL 中抓取一个字符串“page”,并以此为基础抓取我的对象。所以,我的网址如下所示:

http://www.example.com/landing/index/index/page/CoolPage

我目前的想法是将这个 url 构造为 /landing/{page},这样它就可以:

http://www.example.com/landing/CoolPage

起初,我尝试使用 htaccess 来完成此操作。我有以下内容:

RewriteRule ^landing/([a-z\-]+)(/)?$ landing/index/index/page/$1 [R,L]

这可行,但会重定向而不是重写。我也只用[L] 尝试过,最后没有 [],但我只是在我的 404 页面结束。

理想情况下,我会使用配置重写,因为它可以与我的模块一起打包,但我找不到任何像这样使用它们的文档。我会很高兴使用 .htaccess 甚至基于 db 的重写,如果它按需要工作的话。

有没有办法在 Magento 中进行这样的重写?

【问题讨论】:

  • 我现在有一个解决方案,但它涉及添加我自己的路由器。我将把它搁置几天,希望有一种方法可以减少黑客攻击。

标签: .htaccess magento mod-rewrite url-rewriting rewrite


【解决方案1】:

我想我前段时间从客户那里收到了完全相同的请求,这就是我的处理方式,我认为这是最简单和最简单的方法...

实际上,可以直接重写模块的 config.xml 文件。 在这个例子中,所有的 URL 像

http://www.domain.com/landing/whatever

将被改写为

http://www.domain.com/landingpage/page/view/whatever

配置.xml

<?xml version="1.0"?>
<config>
    <modules>
        <My_LandingPage>
            <version>0.0.1</version>
        </My_LandingPage>
    </modules>

    <frontend>
        <routers>
            <landingpage>
                <use>standard</use>
                <args>
                    <module>My_LandingPage</module>
                    <frontName>landingpage</frontName>
                </args>
            </landingpage>
        </routers>
    </frontend>

    <global>
        <!-- Rewrite requested routes -->
        <rewrite>
            <my_landingpage_page_view>
                <from><![CDATA[#^/landing/#]]></from>
                <to>/landingpage/page/view/</to>
                <complete>1</complete>
            </my_landingpage_page_view>
        </rewrite>
</config>

控制器

<?php
class My_LandingPage_PageController extends Mage_Core_Controller_Front_Action {

    /**
     * View page action
     */
    public function viewAction() {

        // Get the requested path (/landing/whatever)
        $pathInfo = $this->getRequest()->getOriginalPathInfo();

        // Extract the requested key (whatever)
        $pathArray = explode('/landing/', $pathInfo);
        $requestedKey = $pathArray[1];

        // So, from there you can use $requestedKey to load any model using it.
        // This is also where you will load and render your layout.

    }
}

关于布局的附注

由于真正调用的控制器动作是“landingpage/page/view”,如果你需要这个模块的一些布局,它的句柄将是&lt;landingpage_page_view&gt;

【讨论】:

    【解决方案2】:

    如果我见过的话,这是一个教科书式的自定义路由器案例。您可以采用 CMS 路由器的方法并调整请求对象上的路径,以便您的控制器可以使用标准路由器进行匹配。

    您的另一种选择是创建一个索引器来为您的模块实体创建重写并将它们存储在core_url_rewrite 表中。

    【讨论】:

      猜你喜欢
      • 2014-02-19
      • 2015-02-28
      • 2016-04-12
      • 2011-06-13
      • 1970-01-01
      • 2014-07-17
      • 2018-12-18
      • 1970-01-01
      • 2015-08-31
      相关资源
      最近更新 更多