看一个url例子 
http://localhost/magento/index.php/customer/account/login 
这里假定http://localhost/magento/ 是magento安装目录。那么Magento将自动转而执行customer模块下名字AccountController的loginAction方法。这个是ZendFramework的默认重写规则。

另外一个产品详细页的例子 
http://localhost/magento/index.php/catalog/product/view/id/1/category/3

Magento将寻址catalog模块的ProductController的viewAction方法,传入参数id和category.

为了搜索引擎更加友好化,我们可能希望用用下面的url同样访问到该产品 
http://localhost/magento/index.php/basketball/tayshaun-prince-signed-auto-baketball-with-coa-gold.html

Magento重写了ZendFramework的机制完成这个功能,它的实现思路是:当发送请求的时候,Magento首先判断 basketball/tayshaun-prince-signed-auto-baketball-with-coa-gold.html是否在数据库core_url_rewrite表中是否有该route path和实际target path映射记录,没有的话,就使用Zend Framework的重写规则寻址相应的php执行代码。

为了窥视它的Rewrite机制, 还着实费了不少力气,但是也只是懂些皮毛。下面说说我分析这个机制的过程:

仍然以访问

  1. http://localhost/magento/index.php/basketball/tayshaun-prince-signed-auto-baketball-with-coa-gold.html

为例

1. 从magento安装目录下的index.php开始,末尾的Mage::run();很显然是程序的入口。 
2. 打开app/Magento.php查看run()函数尝试在不同的位置输出self::_$app属性(是一个类),看看有没有url关键字basketball,结果发现多处子对象有字符串属性包含它:

  1. Mage_Core_Controller_Request_Http
  2. ->_originalPathInfo = "/basketball/tayshaun-prince-signed-auto-baketball-with-coa-gold.html"
  3. ->_requestString = "/basketball/tayshaun-prince-signed-auto-baketball-with-coa-gold.html"
  4. ->_requestUri = "/magento/index.php/basketball/tayshaun-prince-signed-auto-baketball-with-coa-gold.html"
  5. ->_pathInfo = "catalog/product/view/id/1/category/3"

那么为了完成映射,程序必须有一处读取其中一个属性(原始url串),然后进行解析和转换。这是着实有点迷惑了,四个属性,到底哪一个才是关键的呢?

3.打开Mage_Core_Controller_Request_Http类文件,先从_originalPathInfo入手(因为名字带了一个original),找到getOriginalPathInfo(), 该方法调用了setPathInfo(), 这么说这个方法应该是设置_pathInfo的函数了,结果这个发现该方法先调用getRequestUrl方法初始化属性 
_requestUri,这个是url源头,然后运算出如下属性: 
_originalPathInfo 
_requestString 
_pathInfo

4. 接着全目录搜索调用getOriginalPathInfo的类方法,找到类Mage_Core_Controller_Varien_Router_Standard。打开调用方法为match,浏览了下整个函数内容,发现了如下代码

  1. $request->setModuleName($module);
  2. $request->setControllerName($controller);
  3. $request->setActionName($action);

由此可以猜测出,对于该例子,这里应该有如下的映射:

  1. $module = 'catalog';
  2. $controller = 'ProductController';
  3. $action = 'viewAction';

所以下面的就是要找出$moudle变量是如何计算出来的

5. 找到上面初始化$module的位置 
  $module = $request->getModuleName(); 
一定是$request的属性被某处初始化,才使得 $request->getModuleName()返回module名字。 
于是代码定位到前面的 
  $p = explode('/', trim($request->getPathInfo(), '/'));

6. $request是Mage_Core_Controller_Request_Http类的实例,打开它的getPathInfo方法。 
......哈哈

线索中断,其实这种分析方法是不推荐的,完全没有程序员逻辑:)

线索II. 
1. 从Mage::run()方法开始,程序员可以敏感地意识到下面代码的关键性

  1. Mage::run() {
  2. ...
  3. self::app()->getFrontController()->dispatch();
  4. ...
  5. }

2.打开Mage_Core_Controller_Varien_Front的 dispatch方法

  1. class Mage_Core_Controller_Varien_Front{
  2. function dispatch() {
  3. ....
  4. Mage::getModel('core/url_rewrite')->rewrite();
  5. ....
  6. while (!$request->isDispatched() && $i++<100) {
  7. foreach ($this->_routers as $router) {
  8. if ($router->match($this->getRequest())) {
  9. break;
  10. }
  11. }
  12. }
  13. }

3. Mage::getModel('core/url_rewrite')可以定位到Mage_Core_Model_Url_Rewrite类文件

  1. class Mage_Core_Model_Url_Rewrite {
  2. public function rewrite() {
  3. $this->loadByRequestPath($requestCases);
  4. ......
  5. if (!$this->getId()) {
  6. // debug code
  7. var_dump($requestCases);
  8. exit();
  9. return false;
  10. }
  11. ....
  12. $request->setPathInfo($this->getTargetPath());
  13. return true;
  14. }
  15. }

如果 $this->loadByRequestPath($requestCases);从数据库中core_url_rewrite表查找是否有对应的target_path,如果找不到return false(用默认的rewrite机制),这里应该是可以找到targe path的,即 
catalog/product/view/id/1/category/3

4. 继续回到dispatch()方法,代码 if ($router->match($this->getRequest()))中 
$router 是Mage_Core_Controller_Varien_Router_Standard的实例,方法match如下:

  1. public function match(Zend_Controller_Request_Http $request) {
  2. ....
  3. // set values only after all the checks are done
  4. $request->setModuleName($module);
  5. $request->setControllerName($controller);
  6. $request->setActionName($action);
  7. }

上面代码显而易见,到此代码分析结束。

结论:此次分析是推论+推测(瞎蒙,瞎蒙就是全文搜索匹配字符串确定调用和被调用关系),没有使用任何单步调式工具。

那么分析Rewrite机制对于扩展Magento的意义何在呢? 比如,如果你想为产品增加品牌分类,那么你可能需要定义如下的符合SEO的URL 
http://localhost/magento/index.php/ibm/ 
使之映射到 
http://localhost/magento/index.php/catalog/brand/id/2

此时,你就要定义重写机制使两个URL的映射关系保存到数据库表中。

Magento - Rewrite机制一窥的更多相关文章

  1. 页面静态化3 --- 伪静态技术之Apache的rewrite机制

      Apache的rewrite机制: 意思就是,你发送的地址,比如:http://localhost/news-id67.html会被Apache改写成http://localhost/news.p ...

  2. apache的rewrite机制

    当我们使用thinkphp的时候,比如说我们访问一个Test控制器的test方法,http://localhost/index.php/Test/test/1.html,那个这个1是用get方式传递的 ...

  3. Magento事件机制 - Magento Event/Observer

    为了扩展Magento的功能,我们可以重写Magento的代码,但因为代码只能被重写一次,所以当多个模块需要重写同一部分的代码时,就会引起冲突,好在Magento提供了另一种扩展功能的方法:事件机制, ...

  4. Magento 缓存机制简析

    在知道缓存机制前,首先需要知道,Magento的路由机制,这边就不做赘述了,百度一大堆. 下面一个简单的缓存生效流程: A:首先在页面开始时,Magento在app\code\core\Mage\Co ...

  5. apache的rewrite机制配置

    步骤: 1:启用rewrite模块,在默认情况下,没有启用 修改httpd.conf文件 #启动rewrite模块 LoadModule rewrite_module modules/mod_rewr ...

  6. 页面解耦—— 统跳协议和Rewrite引擎

    原文: http://pingguohe.net/2015/11/24/Navigator-and-Rewrite.html 解耦神器 —— 统跳协议和Rewrite引擎 Nov 24, 2015 • ...

  7. Redis提供的持久化机制(二)

    1.前言 Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富.有字符串,链表,集 合和有序集合.支持在服务器端计算集合的并,交和补集 ...

  8. redis持久化机制之AOF与RDB

    什么是redis Redis是一种面向“key-value”类型数据的分布式NoSQL数据库系统,具有高性能.持久存储.适应高并发应用场景等优势.它虽然起步较晚,但发展却十分迅速. redis为何需要 ...

  9. Redis数据持久化机制AOF原理分析一---转

    http://blog.csdn.net/acceptedxukai/article/details/18136903 http://blog.csdn.net/acceptedxukai/artic ...

随机推荐

  1. Java中的线程

    http://hi.baidu.com/ochzqvztdbabcir/item/ab9758f9cfab6a5ac9f337d4 相濡以沫 Java语法总结 - 线程 一 提到线程好像是件很麻烦很复 ...

  2. HTML5离线应用无法更新的定位与解决

    一.些许前提 最近在制作一个Web应用, 其中用到了HTML5的离线应用功能(offline application), 离线应用的概念就不再阐述, 可以查看这两篇文章: http://www.ibm ...

  3. 【TYVJ】1359 - 收入计划(二分)

    http://tyvj.cn/Problem_Show.aspx?id=1359 一开始是一眼看出是二分的,因为这里有单调性,因为取钱是一次取完并且是连续的. 所以最优取法就是准备达到某个价值再取.最 ...

  4. COJ967 WZJ的数据结构(负三十三)

    WZJ的数据结构(负三十三) 难度级别:C: 运行时间限制:7000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大 ...

  5. maven工程代码关联源代码配置

    最近在学习dubbo,在maven构建完成后,需要关联查看一些依赖jar的源码,配置很简单,如下: 勾选windows-Preferences-Maven- Download Artifact Sou ...

  6. 提升 web 应用程序的性能(二)

    最佳实践 本章将略述能帮助您提升 web 应用程序性能的最佳实践. 减少 HTTP 请求数 每个 HTTP 请求都有开销,包括查找 DNS.创建连接及等待响应,因此削减不必要的请求数可减少不必要的开销 ...

  7. java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntim [问题点数:40分,结帖人wangxiaohua_001]

    14:56:10.093 WARN!! Error for /butterfly/plugins/zhonghang/UsefulData/save_usefuldata.bshjava.lang.N ...

  8. git基础及分支

    关于版本控制 git是一种分布版本控制系统,每一主机都保存了完整副本.必杀技是分支. 在Windows可安装git客户端msysgit. git基础 第一次看progit觉得有点不懂,不懂版本控制,一 ...

  9. 【Xamarin Doc】 Introduction to Storyboards 笔记

    http://developer.xamarin.com/guides/ios/user_interface/introduction_to_storyboards/ Segues There are ...

  10. dynamic-link library shared library of functions and resources

    https://msdn.microsoft.com/en-us/library/1ez7dh12.aspx A dynamic-link library (DLL) is an executable ...