例如: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文件中设置一个普通的控制器.
<frontend>
<routers>
<MyCustomModule>
<use>standard</use>
<args>
<module>Example_MyCustomModule</module>
<frontName>customlist</frontName>
</args>
</MyCustomModule>
</routers>
</frontend>
这里使用的正面名称是自定义列表,它始终有效,不应与任何其他正面名称冲突,重写的名称除此之外.现在每当你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();
}
同样,需要更新它们以匹配配置中的更改.
等/的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();
}
}
}
