magento 自定义url路径 和 filter data 小结
背景是往一个第三方的搜索插件里面加入filter功能。
首先是路径,插件自己定义了一个router,类似于cms。那首先说说router好了,从入口一路追查的话,会发现最后进入的是Mage_Core_Controller_Varien_Front类下面的dispatch()函数。
public function dispatch()
{
$request = $this->getRequest(); // If pre-configured, check equality of base URL and requested URL
$this->_checkBaseUrl($request); $request->setPathInfo()->setDispatched(false); $this->_getRequestRewriteController()->rewrite(); Varien_Profiler::start('mage::dispatch::routers_match');
$i = 0;
while (!$request->isDispatched() && $i++ < 100) {
foreach ($this->_routers as $router) {
/** @var $router Mage_Core_Controller_Varien_Router_Abstract */
if ($router->match($request)) {
break;
}
}
}
Varien_Profiler::stop('mage::dispatch::routers_match');
if ($i>100) {
Mage::throwException('Front controller reached 100 router match iterations');
}
// This event gives possibility to launch something before sending output (allow cookie setting)
Mage::dispatchEvent('controller_front_send_response_before', array('front'=>$this));
Varien_Profiler::start('mage::app::dispatch::send_response');
$this->getResponse()->sendResponse();
Varien_Profiler::stop('mage::app::dispatch::send_response');
Mage::dispatchEvent('controller_front_send_response_after', array('front'=>$this));
return $this;
}
前面的代码是把url链接里的各项参数放到request类里面,方便后面调用,从第14行开始,magento会把所有继承自Mage_Core_Controller_Varien_Router_Abstract类的函数拉出来一个个循环match函数,直到找到一个匹配request的。
也就是说假如我想自定义路径,就可以继承Mage_Core_Controller_Varien_Router_Abstract类,然后在match函数里写上自己的路由规则,一旦匹配就跳转到自己想他去的controller类。
不过很显然插件自定义的router并不能很好的完成filter的工作,首先贴上,插件的原来的代码:
public function match(Zend_Controller_Request_Http $request)
{
$identifier = trim($request->getPathInfo(), '/'); $condition = new Varien_Object(array(
'identifier' => $identifier,
'continue' => true,
)); $identifier = $condition->getIdentifier(); if ($condition->getRedirectUrl()) {
Mage::app()->getFrontController()->getResponse()
->setRedirect($condition->getRedirectUrl())
->sendResponse();
$request->setDispatched(true); return true;
} if (!$condition->getContinue()) {
return false;
} $page = Mage::getModel('searchlandingpage/page')->checkIdentifier($identifier);
//代码原理很简单,对url里的路径拆解,然后查询这个路径是否在自定义的表里面,checkIdentifier()就是sql查询。
if (!$page) {
return false;
} $request->setModuleName('searchlandingpage')
->setControllerName('page')
->setActionName('view')
->setParam('q', $page->getQueryText())
->setParam('id', $page->getId());
//若查询到了就设置request里的各项参数,告诉magento跳转到这里去
$request->setAlias(
Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
$identifier
); return true;
}
修改后:
public function match(Zend_Controller_Request_Http $request)
{
$helper = Mage::helper('ajaxpriceslider');
$suffix = Mage::getStoreConfig('catalog/seo/category_url_suffix');
$identifier = ltrim($request->getPathInfo(), '/');
$identifier = substr($identifier, 0, strlen($identifier) - strlen($suffix));
$urlSplit = explode($helper->getRoutingSuffix(), $identifier, 2); if(isset($urlSplit[0])){
$cat = $urlSplit[0];
}else{
$cat = $identifier;
}
//这里主要作用是给自定义的路径添加magento的后缀, 如 xxx.html 之类 $condition = new Varien_Object(array(
'identifier' => $cat,
'continue' => true,
)); $cat = $condition->getIdentifier(); if ($condition->getRedirectUrl()) {
Mage::app()->getFrontController()->getResponse()
->setRedirect($condition->getRedirectUrl())
->sendResponse();
$request->setDispatched(true); return true;
} if (!$condition->getContinue()) {
return false;
} $page = Mage::getModel('searchlandingpage/page')->checkIdentifier($cat); if (!$page) {
return false;
} $request->setModuleName('searchlandingpage')
->setControllerName('page')
->setActionName('view')
->setParam('q', $page->getQueryText())
->setParam('id', $page->getId()); $request->setAlias(
Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
$cat.$suffix
);
// 解析url参数,例如xxx.com/search/chiken_run/shopby/color/brown,green,white-and-green.html的网址,shopby后面的参数会被解析成array('color'=>'brown,green,white-and-green')
$params = explode('/', trim($urlSplit[1], '/'));
$layerParams = array();
$total = count($params);
for ($i = 0; $i < $total - 1; $i++) {
if (isset($params[$i + 1])) {
$layerParams[$params[$i]] = urldecode($params[$i + 1]);
++$i;
}
} $layerParams += $request->getPost();
$request->setParams($layerParams); // 这个生成链接用
Mage::register('layer_params', $layerParams);
$controllerClassName = $this->_validateControllerClassName('Mirasvit_SearchLandingPage', 'page'); $controllerInstance = Mage::getControllerInstance($controllerClassName, $request, $this->getFront()->getResponse());
// 这个是立即执行自己定义的controller类的view函数,不这样做的话,magento会继续加载两个系统router 然后修改了$params,导致我删选数据出问题
$request->setDispatched(true);
$controllerInstance->dispatch('view'); return true;
}
以上路径修改完了,在点击删选的时候 ajax寻址就能寻到了,但是返回的数据还是有问题的,这时候就需要修改 刚刚定义的controller下面的view函数了。
其实修改就一步,将$this->renderLayout();修改成
if ($this->getRequest()->isAjax()) {
$listing = $this->getLayout()->getBlock('search_result_list')->toHtml();//根据页面的layout不同,这里的getBlock也会不同,下面也是
$layer = $this->getLayout()->getBlock('catalogsearch.leftnav')->toHtml();
// Fix urls that contain '___SID=U'
$urlModel = Mage::getSingleton('core/url');
$listing = $urlModel->sessionUrlVar($listing);
$layer = $urlModel->sessionUrlVar($layer);
$response = array(
'listing' => $listing,
'layer' => $layer
);
$this->getResponse()->setHeader('Content-Type', 'application/json', true);
$this->getResponse()->setBody(json_encode($response));
} else {
$this->renderLayout();
}
到这一步删选功能正常了,至于他是如何工作的,我有空补上
magento 自定义url路径 和 filter data 小结的更多相关文章
- Magento 自定义URL 地址重写 分类分级显示
我们打算将URL在分类页面和产品页面分别定义为: domain.com/category/分类名.html domain.com/category/子分类名.html domain.com/goods ...
- Magento中URL路径的获取
//获得 media 带 http 的url 地址. Mage::getBaseUrl('media') //获得skin 和js 目录的地址: Mage::getBaseUrl('skin'); M ...
- 七天学会ASP.NET MVC (六)——线程问题、异常处理、自定义URL
本节又带了一些常用的,却很难理解的问题,本节从文件上传功能的实现引出了线程使用,介绍了线程饥饿的解决方法,异常处理方法,了解RouteTable自定义路径 . 系列文章 七天学会ASP.NET MVC ...
- 线程问题、异常处理、自定义URL
线程问题.异常处理.自定义URL 本节又带了一些常用的,却很难理解的问题,本节从文件上传功能的实现引出了线程使用,介绍了线程饥饿的解决方法,异常处理方法,了解RouteTable自定义路径 . 系 ...
- 根据url路径获取图片并显示到ListView中
项目开发中我们需要从网络获取图片显示到控件中,很多开源框架如Picasso可以实现图片下载和缓存功能.这里介绍的是一种简易的网络图片获取方式并把它显示到ListView中. 本案例实现的效果如下: 项 ...
- Spring Boot 2.X(十):自定义注册 Servlet、Filter、Listener
前言 在 Spring Boot 中已经移除了 web.xml 文件,如果需要注册添加 Servlet.Filter.Listener 为 Spring Bean,在 Spring Boot 中有两种 ...
- tp5 上传图片(自定义图片路径)
控制器调用 /** * [goods_addimg 图片上传] * @return [type] [description] */ public function addimg(){ if (requ ...
- paip.解决中文url路径的问题图片文件不能显示
paip.解决中文url路径的问题图片文件不能显示 #现状..中文url路径 图片文件不能显示 <img src="img/QQ截图20140401175433.jpg" w ...
- JS分页 + 获取MVC地址栏URL路径的最后参数
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...
随机推荐
- python day-3 基本数据类型
1. 编码 1. 最早的计算机编码是ASCII. 美国人创建的. 包含了英文字母(大写字母, 小写字母). 数字, 标点等特殊字符!@#$% 128个码位 2**7 在此基础上加了一位 2**8 8位 ...
- UIView局部点击
今天上班遇到一种情况,需要局部响应点击事件,比如在一个UIImageView中设置一个小圆圈图片,要求点击圆圈里面不响应点击,点击小圆圈外面的部分响应点击.可以通过重写hitTest:withEven ...
- [RK3288][Android6.0] 调试笔记 --- pmu(rk818)寄存器读写【转】
本文转载自:http://blog.csdn.net/kris_fei/article/details/76919134 Platform: Rockchip OS: Android 6.0 Kern ...
- SDUT 周赛 神奇的树(简单题 注意数据类型的溢出 )
神奇的树 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 SDUT有一颗神奇的苹果树.假如某天早上这树上有x个苹果,那么这树这一天 ...
- os、sys和shutil模块
运行环境:python3 OS模块:os 模块提供了一个统一的操作系统的接口函数 下面的path指路径的意思 os.stat(file) #查询文件属性操作 os.sep #取代操作系统特定的路径分隔 ...
- iOS 深拷贝、浅拷贝、自定义对象拷贝简介
copy语法的目的:改变副本的时候,不会影响到源对象: 深拷贝:内容拷贝,会产生新的对象.新对象计数器置为1,源对象计数器不变. 浅拷贝:指针拷贝,不会产生新的对象.源对象计数器+1. 拷贝有下面两个 ...
- MDZX——张能传
「你们到底要干什么?!」——8012年7月13日 张能于MDZX ———————————— 序章 ———————————— 话说天下大势,分久必合,合久必分. 他肩扛99米大砍刀,站在MDZX大门对面 ...
- 09:LGTB 学分块
总时间限制: 10000ms 单个测试点时间限制: 1000ms 内存限制: 65536kB 描述 LGTB 最近在学分块,但是他太菜了,分的块数量太多他就混乱了,所以只能分成 3 块 今天他得 ...
- View Programming Guide for iOS ---- iOS 视图编程指南(五)---Animations
Animations Animations provide fluid visual transitions between different states of your user inter ...
- ASP.NET Core 依赖注入(DI)
ASP.NET Core的底层设计支持和使用依赖注入.ASP.NET Core 应用程序可以利用内置的框架服务将服务注入到启动类的方法中,并且应用程序服务也可以配置注入.由ASP.NET Core 提 ...