商品product,分类category的url 可以通过后台的url_key,以及meta 的信息进行编辑自定义。这里不再重述,主要讲解如何通过自定义url,实现catalogsearch (商品搜索的方法)url rewrite。
方法一:通过设置 event 事件实现,最终实现 /custom_url/query/, /custom_url/query/2.html 的形式
1: local 下创建新的模块 search (Company_Search), 配置文件如下(config.xml):
<?xml version="1.0"?>
<config>
<modules>
<Company_Search>
<version>1.0.1</version>
</Company_Search>
</modules>
<frontend>
<routers>
<search>
<use>standard</use>
<args>
<module>Company_Search</module>
<frontName>custom_url</frontName>
</args>
</search>
</routers>
</frontend>
</config>
2: 添加 event 事件,修改 config.xml
<global>
<events>
<controller_front_init_routers>
<observers>
<search>
<type>singleton</type>
<class>Company_Search_Controller_Router</class>
<method>initControllerObserver</method>
</search>
</observers>
</controller_front_init_routers>
</events>
</global>
3: 创建 Company_Search_Controller_Router class
class Company_Search_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract
{
/**
* Create an instance of the router and add it to the queue
*
* @param Varien_Event_Observer $observer
* @return bool
*/
public function initControllerObserver(Varien_Event_Observer $observer)
{
try {
$routerClass = get_class($this);
$observer->getEvent()
->getFront()
->addRouter('custom_url', new $routerClass);
return true;
}
catch (Exception $e) {
echo $e->getMessage();
}
return false;
}
/**
* Attempt to match the current URI to this module
* If match found, set module, controller, action and dispatch
*
* @param Zend_Controller_Request_Http $request
* @return bool
*/
public function match(Zend_Controller_Request_Http $request)
{
try {
$_uri=urldecode($request->getPathInfo());
$_path=explode('/',trim($_uri,'/'));
$params=array();
if($_path[0]=='custom_url') { // 搜索
if (strpos($_uri, '--')) {// ?cat=categoryid&q=qery // 实现category
$_params = explode('--', $_path[1]);
$params['q'] = $_params[0];
$params['cat'] = trim($_params[1], 'c');
} else {
$params['q'] = $_path[1];
}
if (preg_match('/\/[0-9]+\.html$/',$_uri)) { // 分页处理
$page= rtrim($_path[count($_path)-1],'.html');
$params['p']=$page;
}
$path = 'catalogsearch/result/index';
}
return $this->setRoutePath($path,$params,$_uri);
}
catch (Exception $e) {
}
return true;
}
/**
* Set the path and parameters ready for dispatch
*
* @param array $path
* @param array $params = array
* @return $this
*/
public function setRoutePath($path, array $params = array(),$uri)
{
if (is_string($path)) {
// Legacy
$path = explode('/', $path);
$path = array(
'module' => $path[0],
'controller' => $path[1],
'action' => $path[2],
);
}
$request = Mage::app()->getRequest();
$request->setModuleName($path['module'])
->setRouteName($path['module'])
->setControllerName($path['controller'])
->setActionName($path['action']);
foreach($params as $key => $value) {
$request->setParam($key, $value);
}
$uri = strtolower(trim($uri, '/'));
$request->setAlias(
Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
ltrim($uri, '/')
);
return true;
}
}。
方法二 : 直接在config.xml 中,通过正则的形式匹配
<global>
<rewrite>
<service>
<from><![CDATA[#^/custom_url/(.+)#]]></from>
<to><![CDATA[/catalogsearch/result/index/q/$1/]]></to>
<complete>1</complete>
</service>
</rewrite>
</global>
设置catalogsearch 页面的meta 信息
1: 增加event 事件,此事件
Mage_Core_Controller_Varien_Action,中的renderlayout 方法中的 Mage::dispatchEvent('controller_action_layout_render_before_'.$this->getFullActionName());
<global>
<events>
<controller_action_layout_render_before_catalogsearch_result_index>
<observers>
<company_search>
<type>model</type>
<class>company_search/observer</class>
<method>setMetaInfo</method>
</company_search>
</observers>
</controller_action_layout_render_before_catalogsearch_result_index>
</events>
</global>
2: 创建observer 类
class Company_Search_Model_Observer
{
public function setMetaInfo($observer){
$head=Mage::app()->getLayout()->getBlock('head');
$q=Mage::app()->getRequest()->getParam('q');// 搜索关键词
$p=Mage::app()->getRequest()->getParam('p');
if((int)$p>1){
$str='-Page '.$p;
}else{
$str='';
}
if($head){
$head->setTitle("title")
->setKeywords("keywordps")
->setDescription("description");
}
}
}