如何从后端即系统配置更改自定义模块的URL标识符

kha*_*ato 3 magento magento-1.4 magento-1.5

我想给admin提供更改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>
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

之后我创建了一个模型文件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();

        }

    }

}
Run Code Online (Sandbox Code Playgroud)

在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>
Run Code Online (Sandbox Code Playgroud)

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

    <adminhtml>
        <args>
            <modules>
                <FME_Press_Override before="Mage_Adminhtml">FME_Press_Override_Admin</FME_Press_Override>
            </modules>
        </args>
    </adminhtml>
Run Code Online (Sandbox Code Playgroud)

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

如果你想我也可以添加该代码?

现在,我该怎么办?

clo*_*eek 7

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

您希望它可以更改,因此您无法使用固定的基于XML的重写.相反,让我们看一下内置的重写系统.

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

<frontend>
    <routers>
        <MyCustomModule>
            <use>standard</use>
            <args>
                <module>Example_MyCustomModule</module>
                <frontName>customlist</frontName>
            </args>
        </MyCustomModule>
    </routers>
</frontend>
Run Code Online (Sandbox Code Playgroud)

这里使用的正面名称customlist始终有效,不应与任何其他正面名称冲突,重写的名称除此之外.现在每当你生成一个URL(可能在一个帮助函数中)时,你都会对这个明显固定的正面名称这样做.

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

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

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

$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();
Run Code Online (Sandbox Code Playgroud)

所以现在如果管理员将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...
}
Run Code Online (Sandbox Code Playgroud)

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

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

同样,需要更新它们以匹配配置中的更改.

等/的system.xml

<identifier translate="label">
    <label>SELF URL Identifier</label>
    <frontend_type>text</frontend_type>
    <backend_model>myCustomModule/config_identifier</backend_model>
    ...
</identifier>
Run Code Online (Sandbox Code Playgroud)

型号/配置/ 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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)