gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:09:39 25 4
gpt4 key购买 nike

我想为管理员提供从后端更改 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 中进行一些更改,但我不知道要进行哪些更改。

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

现在,我还应该做什么?

最佳答案

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

你希望它是可变的,所以你不能使用固定的 XML based rewrite .相反,让我们看看内置的重写系统。

首先在您模块的etc/config.xml 文件中设置一个普通的 Controller 。

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

这里使用的前面的名字是customlist,它总是有效的,不应该与任何其他前面的名字冲突,重写的名字应该是除此之外的。现在每当你generate an URL (也许在辅助函数中)你这样做是为了这个明显固定的前面的名字。

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

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

下一步是为每个可能的标识符存储 组合创建重写记录。你可能有有限数量的列表,所以这是可能的,也许标识符是特定于商店的,所以每个只需要定义一次。遍历 an installer script 中的所有列表或者让每个列表在保存时创建重写。

$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();
}

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

etc/system.xml

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

模型/配置/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();
}
}
}

关于magento-1.4 - 如何从后端更改自定义模块的 URL 标识符,即系统配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6621576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com