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