0

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

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

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 中进行这样的重写?

4

2 回答 2

3

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

实际上,可以直接对模块的 config.xml 文件进行重写。在此示例中,所有 URL 都像

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

将被改写为

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

配置文件

<?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”,如果您需要此模块的一些布局,它的句柄将是<landingpage_page_view>.

于 2012-09-06T07:42:18.753 回答
1

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

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

于 2012-09-04T22:44:02.427 回答