我制作了一个带有magento布局的自定义模块。它显示了特定客户群的客户信息。
其网址是
www.mysite.com/profile/index/index/mayank
其中profile是我的modulename,index是控制器名称,2nd index是索引控制器的方法,mayank是客户的用户名(唯一字段或属性)。
我需要的是什么。
将此网址设为: -
www.mysite.com/mayank
答案 0 :(得分:2)
首先,Magento URL倾向于将字段名称放在值之前。所以我希望长网址看起来像这样:
www.mysite.com/profile/index/index/name/mayank
这样可以在控制器中进行整齐的检索,
public function indexAction() {
$name = $this->getRequest()->get('name');
}
“无论如何我们可以做'如果没有找到路线,那么magento会调用我们模块的控制器......?”
我wrestled with routers before我认为除非绝对必要,否则他们是不必要的。当可能的URL数量有限时,提前save a rewrite更容易。这样做的最佳时间是用户名更改。
要收听更改,请将其添加到模块的config.xml
:
<global>
<events>
<customer_save_after>
<observers>
<mayank_profile>
<type>singleton</type>
<class>mayank_profile/observer</class>
<method>customerSaveAfter</method>
</mayank_profile>
</observers>
</customer_save_after>
<customer_delete_after>
<observers>
<mayank_profile>
<type>singleton</type>
<class>mayank_profile/observer</class>
<method>customerDeleteAfter</method>
</mayank_profile>
</observers>
</customer_delete_after>
</events>
</global>
在模块的Model
文件夹中创建Observer.php
:
class Mayank_Profile_Model_Observer {
public function customerSaveAfter(Varien_Event_Observer $observer) {
$customer = $observer->getCustomer();
// username is not a standard attribute, hopefully you have created it
if ($customer->dataHasChangedFor('username')) {
$this->remove($customer->getOrigData('username'));
$this->add($customer->getData('username'));
}
}
public function customerDeleteAfter(Varien_Event_Observer $observer) {
$customer = $observer->getCustomer();
$this->remove($customer->getOrigData('username'));
}
protected function remove($username) {
// this does nothing if rewrite doesn't exist
Mage::getModel('core/url_rewrite')
->loadByIdPath('profile/'.$username);
->delete();
}
protected function add($username) {
if ($this->isValidUsername($username)) {
$storeId = Mage::app()->getDefaultStoreView()->getId();
Mage::getModel('core/url_rewrite')
->setIsSystem(0)
->setStoreId($storeId)
// IdPath is used by remove() function
->setIdPath('profile/'.$username)
// TargetPath is your controller/action
->setTargetPath('profile/index/index/name/'.$username)
// RequestPath is what the browser sees
->setRequestPath($username)
->save();
}
}
protected function isValidUsername($username) {
// feel free to add more checks here
// start with existing rewrites
$rewrite = Mage::getModel('core/url_rewrite')
->loadByRequestPath($username);
if (!$rewrite->isObjectNew()) {
return false;
}
// scan known frontNames
$frontNames = Mage::getConfig()->getXPath('*/routers/*/args/frontName');
foreach ($frontNames as $name) {
if ($username == (string) $name) {
return false;
}
}
return true;
}
}
这是相当多的工作,但这里的优点是以下代码正确查找之前保存为“RequestPath”的路径:
echo Mage::getUrl('profile', array(
'name' => $username,
'_use_rewrite' => true
));
这是一种更简单的方法,仅适用于不能与其他模块冲突的URL。将其添加到您的config.xml
:
<global>
<rewrite>
<mayank_profile>
<from><![CDATA[#^/profile/#]]></from>
<to><![CDATA[/profile/index/index/name/]]></to>
<complete>1</complete>
</mayank_profile>
</rewrite>
</global>
输出URL时,您需要自己连接路径。 e.g。
echo Mage::getUrl('', array(
// Use _direct here to avoid a trailing solidus
'_direct' => 'profile/' . $username
));
答案 1 :(得分:0)
您可以尝试使用htaccess rewriterule
或者
在controller_action_layout_load_before
上创建观察者并获取REQUEST_URI
并将其用于您的案例。