[英]Magento seo friendly url in custom module
I would like to create porfolio module, but I need it to be seo friendly, so url should look like: 我想创建porfolio模块,但是我需要它对seo友好,因此url应该如下所示:
example.com/portfolio/projectid
where portfolio - module, so it will be index action projectid - based on field alias where组合-模块,因此它将是基于字段别名的索引操作projectid
its easy to create example.com/portfolio
but how to create controller, or detect what is after? 创建example.com/portfolio
很容易,但是如何创建控制器,或者检测到之后是什么? The same URL hierarchy is in products, so it's category/product 产品中使用相同的URL层次结构,因此它是类别/产品
Anybody have idea how to do it? 有人知道怎么做吗?
Below code works for item/id. 以下代码适用于项目/ ID。 you can change with your requirement. 您可以根据需要进行更改。
$model = Mage::getModel('items/items');
/*Rewrite */
$isSystem = 0; // set 0 for custom url as we have created custom for profile extension
$_itemId = $model->getModuleItemId();//module_item_id field
$_itemName = $model->getTitle();//title field
$_itemName = strtolower(str_replace(" ", "", $_itemName));
// save profile view url rewrite
$viewIdPath = 'item/id'.'/'.$_itemId;
$viewRequestPath = 'items/'.$_itemName;
$viewTargetPath = 'items/index/item/id/'.$_itemId;//controller is itemsController.php, Action is itemAction()
$_coreUrlRewrite = Mage::getModel('core/url_rewrite');
$_coreUrlRewrite->load($viewIdPath, 'id_path'); // check if item path already saved? If yes, $_coreUrlRewrite will contain existing data.
$_coreUrlRewrite->setStoreId($_storeId)
->setIdPath($viewIdPath)
->setRequestPath($viewRequestPath)
->setTargetPath($viewTargetPath)
->setIsSystem($isSystem)
->save();
if(isset($viewRequestPath)){
$model->setUrl($viewRequestPath);
$model->save();
}
/*Rewrite End*/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.