背景是往一个第三方的搜索插件里面加入filter功能。

首先是路径,插件自己定义了一个router,类似于cms。那首先说说router好了,从入口一路追查的话,会发现最后进入的是Mage_Core_Controller_Varien_Front类下面的dispatch()函数。

  1. public function dispatch()
  2. {
  3. $request = $this->getRequest();
  4.  
  5. // If pre-configured, check equality of base URL and requested URL
  6. $this->_checkBaseUrl($request);
  7.  
  8. $request->setPathInfo()->setDispatched(false);
  9.  
  10. $this->_getRequestRewriteController()->rewrite();
  11.  
  12. Varien_Profiler::start('mage::dispatch::routers_match');
  13. $i = 0;
  14. while (!$request->isDispatched() && $i++ < 100) {
  15. foreach ($this->_routers as $router) {
  16. /** @var $router Mage_Core_Controller_Varien_Router_Abstract */
  17. if ($router->match($request)) {
  18. break;
  19. }
  20. }
  21. }
  22. Varien_Profiler::stop('mage::dispatch::routers_match');
  23. if ($i>100) {
  24. Mage::throwException('Front controller reached 100 router match iterations');
  25. }
  26. // This event gives possibility to launch something before sending output (allow cookie setting)
  27. Mage::dispatchEvent('controller_front_send_response_before', array('front'=>$this));
  28. Varien_Profiler::start('mage::app::dispatch::send_response');
  29. $this->getResponse()->sendResponse();
  30. Varien_Profiler::stop('mage::app::dispatch::send_response');
  31. Mage::dispatchEvent('controller_front_send_response_after', array('front'=>$this));
  32. return $this;
  33. }

前面的代码是把url链接里的各项参数放到request类里面,方便后面调用,从第14行开始,magento会把所有继承自Mage_Core_Controller_Varien_Router_Abstract类的函数拉出来一个个循环match函数,直到找到一个匹配request的。

也就是说假如我想自定义路径,就可以继承Mage_Core_Controller_Varien_Router_Abstract类,然后在match函数里写上自己的路由规则,一旦匹配就跳转到自己想他去的controller类。

不过很显然插件自定义的router并不能很好的完成filter的工作,首先贴上,插件的原来的代码:

  1. public function match(Zend_Controller_Request_Http $request)
  2. {
  3. $identifier = trim($request->getPathInfo(), '/');
  4.  
  5. $condition = new Varien_Object(array(
  6. 'identifier' => $identifier,
  7. 'continue' => true,
  8. ));
  9.  
  10. $identifier = $condition->getIdentifier();
  11.  
  12. if ($condition->getRedirectUrl()) {
  13. Mage::app()->getFrontController()->getResponse()
  14. ->setRedirect($condition->getRedirectUrl())
  15. ->sendResponse();
  16. $request->setDispatched(true);
  17.  
  18. return true;
  19. }
  20.  
  21. if (!$condition->getContinue()) {
  22. return false;
  23. }
  24.  
  25. $page = Mage::getModel('searchlandingpage/page')->checkIdentifier($identifier);
  26. //代码原理很简单,对url里的路径拆解,然后查询这个路径是否在自定义的表里面,checkIdentifier()就是sql查询。
  27. if (!$page) {
  28. return false;
  29. }
  30.  
  31. $request->setModuleName('searchlandingpage')
  32. ->setControllerName('page')
  33. ->setActionName('view')
  34. ->setParam('q', $page->getQueryText())
  35. ->setParam('id', $page->getId());
  36. //若查询到了就设置request里的各项参数,告诉magento跳转到这里去
  37. $request->setAlias(
  38. Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
  39. $identifier
  40. );
  41.  
  42. return true;
  43. }

修改后:

  1. public function match(Zend_Controller_Request_Http $request)
  2. {
  3. $helper = Mage::helper('ajaxpriceslider');
  4. $suffix = Mage::getStoreConfig('catalog/seo/category_url_suffix');
  5. $identifier = ltrim($request->getPathInfo(), '/');
  6. $identifier = substr($identifier, 0, strlen($identifier) - strlen($suffix));
  7. $urlSplit = explode($helper->getRoutingSuffix(), $identifier, 2);
  8.  
  9. if(isset($urlSplit[0])){
  10. $cat = $urlSplit[0];
  11. }else{
  12. $cat = $identifier;
  13. }
  14. //这里主要作用是给自定义的路径添加magento的后缀, 如 xxx.html 之类
  15.  
  16. $condition = new Varien_Object(array(
  17. 'identifier' => $cat,
  18. 'continue' => true,
  19. ));
  20.  
  21. $cat = $condition->getIdentifier();
  22.  
  23. if ($condition->getRedirectUrl()) {
  24. Mage::app()->getFrontController()->getResponse()
  25. ->setRedirect($condition->getRedirectUrl())
  26. ->sendResponse();
  27. $request->setDispatched(true);
  28.  
  29. return true;
  30. }
  31.  
  32. if (!$condition->getContinue()) {
  33. return false;
  34. }
  35.  
  36. $page = Mage::getModel('searchlandingpage/page')->checkIdentifier($cat);
  37.  
  38. if (!$page) {
  39. return false;
  40. }
  41.  
  42. $request->setModuleName('searchlandingpage')
  43. ->setControllerName('page')
  44. ->setActionName('view')
  45. ->setParam('q', $page->getQueryText())
  46. ->setParam('id', $page->getId());
  47.  
  48. $request->setAlias(
  49. Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
  50. $cat.$suffix
  51. );
  52.     // 解析url参数,例如xxx.com/search/chiken_run/shopby/color/brown,green,white-and-green.html的网址,shopby后面的参数会被解析成array('color'=>'brown,green,white-and-green')
  53. $params = explode('/', trim($urlSplit[1], '/'));
  54. $layerParams = array();
  55. $total = count($params);
  56. for ($i = 0; $i < $total - 1; $i++) {
  57. if (isset($params[$i + 1])) {
  58. $layerParams[$params[$i]] = urldecode($params[$i + 1]);
  59. ++$i;
  60. }
  61. }
  62.  
  63. $layerParams += $request->getPost();
  64. $request->setParams($layerParams);
  65.  
  66. // 这个生成链接用
  67. Mage::register('layer_params', $layerParams);
  68. $controllerClassName = $this->_validateControllerClassName('Mirasvit_SearchLandingPage', 'page');
  69.  
  70. $controllerInstance = Mage::getControllerInstance($controllerClassName, $request, $this->getFront()->getResponse());
  71. // 这个是立即执行自己定义的controller类的view函数,不这样做的话,magento会继续加载两个系统router 然后修改了$params,导致我删选数据出问题
  72. $request->setDispatched(true);
  73. $controllerInstance->dispatch('view');
  74.  
  75. return true;
  76. }

以上路径修改完了,在点击删选的时候 ajax寻址就能寻到了,但是返回的数据还是有问题的,这时候就需要修改 刚刚定义的controller下面的view函数了。

其实修改就一步,将$this->renderLayout();修改成

  1.       if ($this->getRequest()->isAjax()) {
  2. $listing = $this->getLayout()->getBlock('search_result_list')->toHtml();//根据页面的layout不同,这里的getBlock也会不同,下面也是
  3. $layer = $this->getLayout()->getBlock('catalogsearch.leftnav')->toHtml();
  4.  
  5. // Fix urls that contain '___SID=U'
  6. $urlModel = Mage::getSingleton('core/url');
  7. $listing = $urlModel->sessionUrlVar($listing);
  8. $layer = $urlModel->sessionUrlVar($layer);
  9.  
  10. $response = array(
  11. 'listing' => $listing,
  12. 'layer' => $layer
  13. );
  14.  
  15. $this->getResponse()->setHeader('Content-Type', 'application/json', true);
  16. $this->getResponse()->setBody(json_encode($response));
  17. } else {
  18. $this->renderLayout();
  19. }

到这一步删选功能正常了,至于他是如何工作的,我有空补上

magento 自定义url路径 和 filter data 小结的更多相关文章

  1. Magento 自定义URL 地址重写 分类分级显示

    我们打算将URL在分类页面和产品页面分别定义为: domain.com/category/分类名.html domain.com/category/子分类名.html domain.com/goods ...

  2. Magento中URL路径的获取

    //获得 media 带 http 的url 地址. Mage::getBaseUrl('media') //获得skin 和js 目录的地址: Mage::getBaseUrl('skin'); M ...

  3. 七天学会ASP.NET MVC (六)——线程问题、异常处理、自定义URL

    本节又带了一些常用的,却很难理解的问题,本节从文件上传功能的实现引出了线程使用,介绍了线程饥饿的解决方法,异常处理方法,了解RouteTable自定义路径 . 系列文章 七天学会ASP.NET MVC ...

  4. 线程问题、异常处理、自定义URL

    线程问题.异常处理.自定义URL   本节又带了一些常用的,却很难理解的问题,本节从文件上传功能的实现引出了线程使用,介绍了线程饥饿的解决方法,异常处理方法,了解RouteTable自定义路径 . 系 ...

  5. 根据url路径获取图片并显示到ListView中

    项目开发中我们需要从网络获取图片显示到控件中,很多开源框架如Picasso可以实现图片下载和缓存功能.这里介绍的是一种简易的网络图片获取方式并把它显示到ListView中. 本案例实现的效果如下: 项 ...

  6. Spring Boot 2.X(十):自定义注册 Servlet、Filter、Listener

    前言 在 Spring Boot 中已经移除了 web.xml 文件,如果需要注册添加 Servlet.Filter.Listener 为 Spring Bean,在 Spring Boot 中有两种 ...

  7. tp5 上传图片(自定义图片路径)

    控制器调用 /** * [goods_addimg 图片上传] * @return [type] [description] */ public function addimg(){ if (requ ...

  8. paip.解决中文url路径的问题图片文件不能显示

    paip.解决中文url路径的问题图片文件不能显示 #现状..中文url路径 图片文件不能显示 <img src="img/QQ截图20140401175433.jpg" w ...

  9. JS分页 + 获取MVC地址栏URL路径的最后参数

    @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...

随机推荐

  1. Swift开发教程--怎样播放图片动画

    废话少说,直接上代码: var barsAnim = UIImageView(frame: self.view.frame); barsAnim.animationImages = NSArray() ...

  2. xamarin.android listview绑定数据及点击事件

    前言 listview是用来显示数据列表的一个控件,今天给大家带来如何使用cursor进行数据绑定以及点击事件. 导读 1.如何创建一个listview 2.如何使用cursor进行绑定数据 3.li ...

  3. MapReduce算法形式五:TOP—N

    案例五:TOP—N 这个问题比较常见,一般都用于求前几个或者后几个的问题,shuffle有一个默认的排序是正序的,但如果需要逆序的并且暂时还不知道如何重写shuffle的排序规则的时候就用以下方法就行 ...

  4. 李雅普诺夫函数 LyapunovFunction 李雅普诺夫意义下的稳定性

    https://zh.wikipedia.org/zh-hans/李亞普諾夫函數 李雅普诺夫函数(Lyapunov function)是用来证明一动力系统或自治微分方程稳定性的函数.其名称来自俄罗斯数 ...

  5. JAVA变量存储

    1.java变量存储域 java变量的存储区域主要放在以下几个地方: (1)寄存器:可以说是最快的存储区,在C/C++中可以声明寄存器变量,但是在java中不能声明寄存器变量,只是编译器在编译时确定. ...

  6. struts2 过滤器

    Chain.doFilter的作用就是继续请求的传递,可传递给下一个filter也可传递给目标页面 如左侧传递给filter2,但fiter2使用上面或者下面的方法将倾情重定向到一个新的页面,而不再传 ...

  7. ActiveMQ P2P模型 观察者消费

    生餐者: package clc.active.listener; import org.apache.activemq.ActiveMQConnectionFactory; import org.t ...

  8. 没有该栏目数据可能缓存文件(data/cache/inc_catalog_base.inc)没有更新请检查是否有写入权限

    dedecms系统搬家后或在系统还原后,重新更新栏目或文件的时候,有时会出现这样的错误提示:没有该栏目数据可能缓存文件(data/cache/inc_catalog_base.inc)没有更新请检查是 ...

  9. hdu1226

    hdu1226 :点击打开题目链接 本题目由于题目意思,容易得知是一道广搜的题目. 首先. 我们需要知道 ,大数取模,比如 如何判断1234567 对15 取模的数为多少?答案是7,但是如果他是大数怎 ...

  10. Opencv:10个步骤检测出图片中条形码

    1. 原图像大小调整,提高运算效率 2. 转化为灰度图 3. 高斯平滑滤波 4.求得水平和垂直方向灰度图像的梯度差,使用Sobel算子 5.均值滤波,消除高频噪声 6.二值化 7.闭运算,填充条形码间 ...