-- Simon R Jones <[EMAIL PROTECTED]> wrote
(on Wednesday, 13 February 2008, 05:14 PM +0000):
> I'm using $this->_forward() in a controller method to forward the request 
> onto a login page, this can't be done that easily in pre-dispatch since 
> different methods have different requirements for ACL and we have a rather 
> complex custom route system going on (part of which happens in specific 
> controllers). At present we're not using Zend ACL.
>
> It seems the default behaviour of _forward() is to continue processing the 
> current method's code, and then forward to the new controller/action. So if 
> multiple forwards exist in that method's code, only the last one actually 
> works.
>
> Is this correct? I had presumed _forward() would forward to the new 
> controller/action and exit the code at that specific point. Is this 
> possible? Or do I have to just return from the method at that point in the 
> code? I.e.
>
>   $this->_forward("index", "login");
>   return;

Correct -- you need to return. I actually usually write it as:

    return $this->_forward('index', 'login');

as the return value of an action is ignored anyways.

The reason it works this way (i.e., you need to return when forwarding)
is because you can't have a called function force the calling function
to return -- it's a language limitation. 

> Further to this, if the above is the recommended way to forward and quit 
> the current method then this of course becomes problematic if I abstracted 
> the login check. I.e. if I called a function to do something like:
>
>   $this->checkLogin();
>
> And the function checkLogin() did the necessary ACL check and also included 
> the forward() - to avoid DRY.

Unfortunate, but necessary. I typically have methods like this return a
boolean, so I can then do something like this:

    if (!$this->checkLogin() {
        return $this->_forward('index', 'login');
    }

-- 
Matthew Weier O'Phinney
PHP Developer            | [EMAIL PROTECTED]
Zend - The PHP Company   | http://www.zend.com/

Reply via email to