Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework, what $this->_forward is doing

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 ?

like image 636
RageZ Avatar asked Oct 30 '09 01:10

RageZ


People also ask

How do I redirect to another page in Zend Framework?

Either use Zend_Controller_Action_HelperBroker to get the redirect helper or do the redirect directly from the Request object.

What happened Zend Framework?

No, Zend Framework is not dead. It has essentially been rebranded as the Laminas Project under the Linux Foundation. The code is available today. All of the versions from 2.0 forward of the Zend Framework project, including Zend MVC applications.


4 Answers

_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.

like image 159
smack0007 Avatar answered Oct 19 '22 12:10

smack0007


My experience with Zend is limited and I hope I'm not showing you something you've already seen but according to the docs (12.7.6. Utility Methods):

_forward($action, $controller = null, $module = null, array $params = null): perform another action. If called in preDispatch(), the currently requested action will be skipped in favor of the new one. Otherwise, after the current action is processed, the action requested in _forward() will be executed.

So it sounds like the context of when it's called matters. In the latter case it will first execute the action from which it's been called then execute the forwarded action. The exception is when it's being called from the preDispatch handler

like image 35
Karim Avatar answered Oct 19 '22 12:10

Karim


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.

like image 29
Dan Avatar answered Oct 19 '22 12:10

Dan


Forward is ment to be used when external redirect is not the right options. Use case (bit ankward, but best i can make up): You have a form that can add your pet (either dog or cat). You have different models for each. You include a select in your form to select dog / cat. Then in your action you do:

if($form->isValid($_POST)){  
  switch($form->select->getValue()){
    case "dog":
      $this->_forward('add-dog','pets','default');
      break;
    case "cat":
      $this->_forward('add-cat','pets','default');
      break;    
  }
}

And you handle different things for cats and dogs in separate actions. The advantage of this is that ALL the parameters are sent along. In constrast when you'd used $this->_redirect() the POST parameters will be lost. That is in some cases intended behaviour (for example after adding a comment you make a redirect to comments list page to avoid double posts and the message "page needs to send data again...".

like image 39
Tomáš Fejfar Avatar answered Oct 19 '22 14:10

Tomáš Fejfar