3

我想为管理员提供MyCustomModule从后端更改 URL 标识符的选项。

例如:www.mydomain.com/identifier

我所做的是以下内容:

etc/system.xml

<identifier translate="label">
    <label>SELF URL Identifier</label>
    <frontend_type>text</frontend_type>
**<backend_model>press/config_identifier</backend_model>** // edited after answer
    <sort_order>1</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>1</show_in_store>
    <comment>(eg: domain.com/identifier)</comment>
</identifier>

helper/data.php

public function getUrl($identifier = null)
{

    if (is_null($identifier)) {
        $url = Mage::getUrl('').self::getListIdentifier();
    } else {
        //$url = Mage::getUrl(self::getListIdentifier()).$identifier;
**$url = Mage::getUrl(self::getListIdentifier(), array('identifier' => $identifier,'_use_rewrites'=>true)); //edited
        }**
    return $url;
}

之后我创建了一个模型文件 identifier.php :

class FME_Press_Model_Config_Identifier extends Mage_Core_Model_Config_Data

{

    protected function _afterSave()

    {

        if ($this->isValueChanged()) {

            $path = $this->getValue();



            // for each $store and $id combination...



            Mage::getModel('core/url_rewrite')

                ->loadByIdPath('press/'.$store.'/'.$identifier)

                ->setRequestPath($path.'/'.$identifier)

                ->save();

        }

    }

}

在 config.xml 我写了这个:

<events>
    <controller_front_init_routers>
        <observers>
            <press>
                <type>singleton</type>
                <class>FME_Pres_Controller_Router</class>
                <method>initControllerRouters</method>
            </press>
        </observers>
    </controller_front_init_routers>
</events>

这也存在于我的文件中,我不确定它是否相关:

    <adminhtml>
        <args>
            <modules>
                <FME_Press_Override before="Mage_Adminhtml">FME_Press_Override_Admin</FME_Press_Override>
            </modules>
        </args>
    </adminhtml>

注意:我被告知要进行一些更改,Controller/Router.php但我不知道要进行哪些更改。

如果您愿意,我也可以添加该代码吗?

现在,我还应该做什么?

4

1 回答 1

7

我觉得更改应用程序的路由器是完全错误的做法。如果另一个模块出于类似目的覆盖它,它会很混乱并且很容易被破坏。干净的方法是使用 URL 重写。

您希望它是可更改的,因此您不能使用固定的基于 XML 的重写。相反,让我们看看内置的重写系统。

首先在你模块的etc/config.xml文件中设置一个普通的控制器。

<frontend>
    <routers>
        <MyCustomModule>
            <use>standard</use>
            <args>
                <module>Example_MyCustomModule</module>
                <frontName>customlist</frontName>
            </args>
        </MyCustomModule>
    </routers>
</frontend>

这里使用的前名是customlist,它将始终有效并且不应与任何其他前名冲突,重写的名称应在此之外。现在,每当您生成一个 URL(可能在辅助函数中)时,您都会对这个明显固定的前端名称执行此操作。

$url = Mage::getUrl('customlist', array(
    'id' => $id,    // 'id' will get used in the "target path" later
    '_use_rewrites' => true
));

请注意,变量标识符 ( $id) 被传递给getUrl函数,而不是简单地附加到它的结果。如果函数返回带有查询 ( &) 或片段 ( #) 的 URL,则标识符可能已附加到错误的部分。

下一步是为identifierstore的每个可能组合创建重写记录。您可能有有限数量的列表,所以这是可能的,也许标识符是特定于商店的,因此每个只需要定义一次。在安装程序脚本中循环遍历所有列表,或者在保存每个列表时创建重写。

$path = Mage::getStoreConfig('custom/config/identifier', $storeId);
// Change 'custom/config/identifier' to match the path used in system.xml

$rewrite = Mage::getModel('core/url_rewrite')
               ->loadByIdPath('customlist/'.$store.'/'.$id);
if ($rewrite->getId()) {
    // A rewrite already exists, you might want to skip creating another
    continue;
}

Mage::getModel('core/url_rewrite')
    ->setStoreId($storeId)
    ->setIsSystem(true) // set to false to allow admin to edit directly
    ->setOptions('RP') // Redirect Permanent 301
    ->setIdPath('customlist/'$storeId.'/'.$id) // should never change
    ->setTargetPath('customlist/index/index/id/'.$id) // what gets used
    ->setRequestPath($path.'/'.$id) // the path used in the browser
    ->save();

因此,现在如果管理员将 URL 路径设置为“foo/bar”并请求页面“www.mydomain.com/foo/bar/3”,它将被重写为“customlist/index/index/id/3”并且该方法Example_MyCustomModule_IndexController::indexAction()将被调用。包含该文件的文件当然是app/code/local/Example/MyCustomModule/controllers/IndexController.php并在3那里检索值:

public function indexAction()
{
    $id = $this->getRequest()->getParam('id'); // 'id' was specified in getUrl()
    // use $id here...
}

它现在应该可以工作了,但是如果一个列表被删除了怎么办?需要为每个商店更新重写。模型有一个_beforeDelete方法,为您的列表对象覆盖它。

protected function _beforeDelete()
{
    Mage::getModel('core/url_rewrite')
        ->loadByIdPath('customlist/'.$storeId.'/'.$this->getId())
        ->delete();
    return parent::_beforeDelete();
}

同样,它们需要更新以匹配配置更改。

等/system.xml

<identifier translate="label">
    <label>SELF URL Identifier</label>
    <frontend_type>text</frontend_type>
    <backend_model>myCustomModule/config_identifier</backend_model>
    ...
</identifier>

模型/配置/标识符.php

class Example_MyCustomModule_Model_Config_Identifier
    extends Mage_Core_Model_Config_Data
{
    protected function _afterSave()
    {
        if ($this->isValueChanged()) {
            $path = $this->getValue();
            // for each $store and $id combination...
            Mage::getModel('core/url_rewrite')
                ->loadByIdPath('customlist/'.$store.'/'.$id)
                ->setRequestPath($path.'/'.$id)
                ->save();
        }
    }
}
于 2011-07-09T18:14:25.710 回答