magento的最新版本已經是1.6了.當然相對於1.5來說在性能上沒有太多的改進,不過修正了不少BUG.同時也添加了不少新的模塊,如果擔心性能,沒用的模塊可以關閉掉.歸入正題.
近日,發現google收錄了不少magento網站的站內搜索結果頁.類似
catalogsearch/result/index/?q= 站內搜索頁面
tag/product/list/tagId/1/ 標籤產品列表頁
這樣的頁面是用戶行爲產生的,對搜索引擎來講,一來表明這個站有更新,二來它是用戶數據,應該說是比較有一定質量的吧.但是在社區的版本中,這兩類用戶數據產生的頁面除了有設置標題之外,沒有設置meta_keyword,meta_description,這樣在google看來,站內存在了具有重複的頁面.而且URL也顯得不是那麼友好.如何改進呢.標題和meta_description好辦.當然不能直接修改代碼.只要把需要修改的block複製到local code pool修改就可以.但是修改url時,要修改magento的tag模塊的product controller類.比如
要把tag/product/list/tagId/1/ 修改成
tag/product/list/tagName/black/這樣的格式的話
如何override magento的tag模塊下的product 這個controller呢?
step 1: 創建如下目錄結構
step 2: 創建模塊的config.xml文件,
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Company_Tag>
<version>0.1.0</version>
</Company_Tag>
</modules>
<frontend>
<routers>
<tag>
<args>
<modules>
<Company_Tag before="Mage_Tag">Company_Tag</Company_Tag>
</modules>
</args>
</tag>
</routers>
</frontend>
</config>
Step 3, 向magento註冊新模塊,在app/etc/modules/上下創建Company_All.xml文件(注,文件名不限,不過爲了區別和可讀性,請按一定規則)
<?xml version="1.0"?>
<config>
<modules>
<Company_Tag>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Tag/>
</depends>
</Company_Tag>
</modules>
</config>
Step 4, 清空緩存看是否在magento後臺有看到註冊的新模塊
Step 5, 確認一切正常後,開始進入到關鍵的代碼階段了,複製一下原來的magento tag模塊的ProductController類的代碼文件到新建模塊對應的目錄下:
即: Mage/Tag/controllers/ProductController.php ----> Company/Tag/controllers/ProductController.php 並做相應修改,代碼如下:
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Mage
* @package Mage_Tag
* @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Tagged products controller
*
* @category Mage
* @package Mage_Tag
* @author Xinhao Zheng <[email protected]>
*/
require_once 'Mage/Tag/controllers/ProductController.php';
class Company_Tag_ProductController extends Mage_Tag_ProductController
{
public function listAction()
{
$tagId = $this->getRequest()->getParam('tagId');
if (!empty($tagId)) {
$tag = Mage::getModel('tag/tag')->load($tagId);
} else {
$tagName = $this->getRequest()->getParam('tagName');
$tag = Mage::getModel('tag/tag')->loadByName($tagName)->load();
}
if(!$tag->getId() || !$tag->isAvailableInStore()) {
$this->_forward('404');
return;
}
Mage::register('current_tag', $tag);
$this->loadLayout();
$this->_initLayoutMessages('checkout/session');
$this->_initLayoutMessages('tag/session');
$this->renderLayout();
}
}
Step 6: 這樣controller的修改就算完成了.也是比早期的版本簡單多了.不過這裏的修改只是實現了它可以財時處理tag/product/list/tagId/1/ 和tag/product/list/tagName/black這樣的頁面,還需要修改相應的block,模板文件以及layout來做調整.比較簡單,就不再做複述了.
進一步,應該要實現下面這樣的,應該已經有模塊實現了,不過可以用來做練習:
catalogsearch/result/index/?q=keyword&p=N&limit ---> catalogsearch/keyword_pageNumber_limit.html
tag/product/list/tagId/1/ ----> tag/tagname_pageNumber_limit.html