What $this->_forward is doing
2010-08-24 10:22
477 查看
Question:
Hello everyone,
I would like someone to explain me what _forward is exactly doing, I cannot see if _forward is also rendering the attached view to the action or just executing the action.
Also is it possible to pass argument to $this->action in a view script ?
More generally my problem is how to code a confirmation page, let's say the user input some stuff and you want to show him confirmation, is forward is mean for that case ?
Answer1:
_forward is an internal redirect. Where as _redirect sends a header that tells the client's browser to go to some other URL, _forward tells the Dispatcher to internally redirect the request somewhere else.
If you consider the normal dispatch order of:
Calling _forward at any point in that progression will cause the following steps to not be executed. So if you call _forward in preDispatch(), someAction() will not be called and so on. If you _forward() in someAction() and you are using the viewRenderer action helper to render your views (you are letting the framework choose what view script to render), then no view script will be rendered in someAction().
When the request is forwarded to the new Controller / Module the entire dispatch process will be repeated there.
You can find out what action is being dispatched by using:
$action will be the url form of the action so if the method is name 'someKindOfAction', $action will contain 'some-kind-of'. You can do this as well for controllers and modules.
Answer2:
I think it's important to note that _forward is very inefficient, and you should always call your method directly. When you do a _forward, the init(), pre and post dispatch run again. Depending on what you have in your init, you can run (and insert) the same database record twice.
It is easy to use but wasteful. If you profile your code, and are banging your head to why everything is being called twice, _forward is the reason. If your like me and you instantiate a few objects in the init() for use throughout the class, you wind up instantiating everything twice! I did load testing on my code and I got better performance by calling the action name directly, like foo(), instead of _forward('foo');
Another off topic tip I think most people know, is it use single quotes wherever possible, sine the PHP parser has to check a string for embedded variables. I don't know how much real world performance this will give, especially if you are using an opcode cache, but it's a best practice.
Hello everyone,
I would like someone to explain me what _forward is exactly doing, I cannot see if _forward is also rendering the attached view to the action or just executing the action.
Also is it possible to pass argument to $this->action in a view script ?
More generally my problem is how to code a confirmation page, let's say the user input some stuff and you want to show him confirmation, is forward is mean for that case ?
Answer1:
_forward is an internal redirect. Where as _redirect sends a header that tells the client's browser to go to some other URL, _forward tells the Dispatcher to internally redirect the request somewhere else.
If you consider the normal dispatch order of:
preDispatch() someAction() postDispatch()
Calling _forward at any point in that progression will cause the following steps to not be executed. So if you call _forward in preDispatch(), someAction() will not be called and so on. If you _forward() in someAction() and you are using the viewRenderer action helper to render your views (you are letting the framework choose what view script to render), then no view script will be rendered in someAction().
When the request is forwarded to the new Controller / Module the entire dispatch process will be repeated there.
You can find out what action is being dispatched by using:
$action = $this->getRequest()->getParam('action');
$action will be the url form of the action so if the method is name 'someKindOfAction', $action will contain 'some-kind-of'. You can do this as well for controllers and modules.
Answer2:
I think it's important to note that _forward is very inefficient, and you should always call your method directly. When you do a _forward, the init(), pre and post dispatch run again. Depending on what you have in your init, you can run (and insert) the same database record twice.
It is easy to use but wasteful. If you profile your code, and are banging your head to why everything is being called twice, _forward is the reason. If your like me and you instantiate a few objects in the init() for use throughout the class, you wind up instantiating everything twice! I did load testing on my code and I got better performance by calling the action name directly, like foo(), instead of _forward('foo');
Another off topic tip I think most people know, is it use single quotes wherever possible, sine the PHP parser has to check a string for embedded variables. I don't know how much real world performance this will give, especially if you are using an opcode cache, but it's a best practice.
相关文章推荐
- What is the difference between angle bracket < > and double quotes " " while including header files
- What is the name of the “-->” operator?(Stackoverflow)
- reason: '[<__NSDictionary0 0x7fda88f00c90> setValue:forUndefinedKey:]: this class is not key value c
- 解决异常:IllegalStateException: Fragment <ThisFragment> is not currently in the FragmentManager
- What static_cast<> is actually doing
- What is /dev/null 2>&1?
- iOS [<NSObject 0x170204110> setValue:forUndefineKey:]: this class is not key value coding-compliant
- What does '>/dev/null 2>&1' mean in this article of crontab basics
- 学习OpenCV 第二版<1-3>:What Is Computer Vision?
- JS:what is this "Object.prototype" anyway...?
- Thrown by JAXB : Property "Any" is already defined. Use &lt;jaxb:property> to resolve this conflict.
- MOSS2007-404 error>"This webpage is not available"
- Packet for query is too large (1986748 > 1048576). You can change this value on the server by
- 在对话框中进行消息映射((CEdit*)GetDlgItem(IDC_EDIT_TEST))->SetWindowText("this is a edit box")
- 求解xilinx ISE使用报错的处理——Port <clk> has illegal connections. This port is connected to an input buffer
- A easy example to tell you what is "2>&1" in Perl
- [Quora] What is the most elegant line of code you've seen?
- This&nbsp;system&nbsp;is&nbsp;not&nbsp;registered&nbsp;wi…
- 独立思考Justic:This Land is My Land & Consenting
- Host '2003SERVER' is not allowed to connect to this MySQL server