controller override and request rewrite in magento-尊龙官方平台

controller override and request rewrite in magento

el/2024/3/25 14:29:00

controller override and request rewrite in magento

there are three ways to override controller in magento. they fit for various purposes.

the first and easiest way can be used to route the request to more than one module. when a request arrives on a frontname, it usually is rounted to a module. for example, /cms/page/view is routed to cms module page controller view action. if i have developed a cms related module called “faq” with a brand new controller “questioncontroller”, but i want it share the same cms frontname with cms module, i.e., i want /cms/question/any_action be routed to faq module.

it is very easy to achieve by a config.xml like the following:

view source print ?
01 < config >
02      < frontend >
03          < routers >
04              < cms >
05                  < args >
06                      < modules >
07                          < any_name >mynamespace_faq any_name >
08                      modules >
09                  args >
10              cms >
11          routers >
12      frontend >
13 config >

strictly speaking, no overriding in above example because it only activates a brand new controller. in a truly overriding example, if i want pagecontroller of faq module override pagecontroller in cms module, i can add before=”mage_cms” to make sure pagecontroller of faq module supersede the same name controller in cms module. the complete configuration is shown below:

view source print ?
01 < config >
02      < frontend >
03          < routers >
04              < cms >
05                  < args >
06                      < modules >
07                          < any_name before = "mage_cms" >mynamespace_faq any_name >
08                      modules >
09                  args >
10              cms >
11          routers >
12      frontend >
13 config >

the second way can be used to mass override controllers.

view source print ?
01 < config >
02      < global >
03          < rewrite >
04              < any_name >
05                  < from > from >
06                  < to >/faq/ to >
07                  < complete > complete >
08              any_name >
09          rewrite >
10      global >
11 config >

the above configuration makes all controllers in faq module override same name controllers in cms module. in class mage_core_controller_varien_front, there is

view source print ?
1 $pathinfo = preg_replace( $from , $to , $request ->getpathinfo());

doing path info string replacement based on regular expression. and because it is based on regular expression, i can do mass replacement at a time.

the tag flags whether requested path info should be turned into new module path info. in other words, when the request arrives on /cms/page/view, it is rewritten to /faq/page/view. without tag, the action layout handle is cms_page_view; with tag, the action layout handle is faq_page_view.

i can use this method to route the request to a brand new controller, i.e.

view source print ?
01 < config >
02      < global >
03          < rewrite >
04              < any_name >
05                  < from > from >
06                  < to >/faq/question/ to >
07                  < complete > complete >
08              any_name >
09          rewrite >
10      global >
11 config >

note that cms module does not have question controller, but the above configuration will not cause any error.

the third way can be used to override individual actions.

view source print ?
01 < config >
02      < global >
03          < routers >
04              < cms >
05                  < rewrite >
06                      < page >
07                          < to >faq/question to >
08                          < override_actions >true override_actions >
09                          < actions >
10                              < view_action >< to >new_module/new_controller/new_action view_action >
11                          actions >
12                      page >
13                  rewrite >
14              cms >
15          routers >
16      global >
17 config >

this method is documented in class mage_core_controller_varien_action.

* this will override:
* 1. cms_module/page_controller/view_action to new_module/new_controller/new_action
* 2. all other actions of cms_module/page_controller to faq_module/question_module

it is very handy to precisely control the request rewrite to action level, but it can not route to a brand new controller. the following code will cause an error.

view source print ?
01 < config >
02      < global >
03          < routers >
04              < cms >
05                  < rewrite >
06                      < question >
07                          < to >faq/question to >
08                          < override_actions >true override_actions >
09                          < actions >
10                              < view_action >< to >new_module/new_controller/new_action view_action >
11                          actions >
12                      question >
13                  rewrite >
14              cms >
15          routers >
16      global >
17 config >

http://www.ngui.cc/el/5126496.html

相关文章

what does backend type static mean in magento?

what does backend type static mean in magento? posted on january 28, 2011 by 芳草苑主 about a year ago, i noticed some category or product attributes, for example, sku, path, etc., were given backend type “static” in class mage_catalog_model_resource_…

is appending block by handle possible in magento layout xml?

is appending block by handle possible in magento layout xml? posted on february 17, 2011 by 芳草苑主 in the product list on magento category page, i have a deeply nested blocks for each product in the list. the structure is something like: view source p…

扎西拉姆的诗

《扎西拉姆的诗》 你见,或者不见我 我就在那里   不悲不喜 你念,或者不念我 情就在那里   不来不去 你爱,或者不爱我 爱就在那里   不增不减 你跟,或者不跟我 我的手就在你手里   不舍不弃 来我的怀里 …

grid报错clause is ambiguous问题-----------------摘自精东

magento深入理解-grid报错clause is ambiguous问题 我记得之前没少讲过grid,今天在写grid遇到这样一个错误,将解决办法分享下。 sqlstate[23000]:integrity constraint violation:1052column increment_idin where clause is ambiguous 这个…

有点累了!

是不是自己累了,是不是真的累了!! 天天就搞些工作的事情,弄的自己没一点幸福感,对于工作带来的喜悦也是不会持续很久!半天就散 没有对象的自己,对父亲这个词语感觉很亲切,很想成为…

magnto获取当前店铺和店铺配置的方法---转

magnto获取当前店铺和店铺配置的方法 开发中常常用到获取当前店铺的id或者获取当前店铺的配置。其实非常的简单,看代码: 1、获取店铺 $storemage::app()->getstore();你可以print一下$store,它是mage_core_model_store对象,…

关于magento的时间/时区问题--------转自精东

关于magento的时间/时区问题 今天同事研究为啥保存的时间不是东八区时间,由此引来的话题。 magento整个系统中使用utc时间,即通用协调时(utc, universal time coordinated)。这样保证在不同的视图或者store里使用的时间与本地时间一致,在系…

magento如何使用和设置cookiesession

magento如何使用和设置cookie&session 给大家介绍两个magento的核心对象-mage_core_model_cookie & mage_core_model_session 首先介绍mage_core_model_cookie,这个对象主要是用来设置cookie的,里面主要下列方法: publi…

magento数据转移/换服务器/换域名

magento数据转移/换服务器/换域名 1.进入原来的phymyadmin,选择magento的那个数据库,在右边的标签上点击导出。 2.全选所有的表,结构那儿都勾上:添加 drop table,如果不存在就增加,添加 auto_increment 值…

magento -- 客户无法使用contact us 发邮件

magento -- 客户无法使用contact us 发邮件 - unable to submit your request. please, try again later magento的contact us是一个非常重要的一个功能,当在线客服不在时,客户可以通过contact us发送邮件来联系尊龙官方平台。问题是,总是会出来这…
网站地图