Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-10-2007, 07:20 AM
Junior Member
 
Join Date: Jul 2007
Posts: 5
Default Trouble with Zend_Controller_Action::_forward

Hi all,

I'm doing some experimentation with Zend Framework before using it on my next app-building project, and I've run into a bit of a snag. My controller scheme is as follows:
  • AdminController
    • indexAction
    • ... additional actions...
    • loginAction

Right now, all the actions do is print out some debugging text, just to let me know they're operating. Since this is obviously mimicking the password-protected, authenticated-users-only section of the app, I have a pre-dispatch method defined for AdminController that should check the user's credentials and forward to loginAction if they haven't yet logged in. So far, though, I'm just forwarding with indiscretion as a matter of testing, but I get an exceptionally long script execution followed by a 503 Internal Server Error every time...!

Here's my (very simple) pre-dispatch method:

Code:
public function preDispatch ( )
{
    $this->_forward('login');
} // END preDispatch
Before you ask, I've commented out that call and checked all the natural routes manually;
Code:
admin/index
and
Code:
admin/login
work just fine on their own. I can even put a call to _forward in an action method and get the desired result, but in preDispatch() the script a-splodes.

I know the culprit isn't really Zend_Controller_Action::_forward() but actually one of the methods in the [i]Zend_Controller_Request[i] family, because I've tried
Code:
$this->getRequest()->setActionName('login')->setDispatched(false);
and
Code:
echo "Pre Dispatch, ";
in my preDispatch() method.

Guess which one works? Hint: it's the short one. Anybody got a clue?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 07-10-2007, 07:36 AM
Junior Member
 
Join Date: Jul 2007
Posts: 5
Default I'm an idiot...

Never occurred to me that I should check the request object for loginAction before forwarding it on back to loginAction... This is what I get for late-night progging with brand new, sharp and shiny tools.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-10-2007, 08:28 AM
Junior Member
 
Join Date: Jul 2007
Posts: 5
Default In retrospect...

I'm not such an idiot, I think. Don't you folks think that one of the basic features of Zend_Controller_Action::_forward() (and _redirect(), for that matter) should be to forbid or ignore a forward/redirect to the original request? This would certainly cut down on the newb mistakes (mine inclusive) and just makes sense, darn it...!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-10-2007, 10:06 PM
SpotSec's Avatar
Senior Member
 
Join Date: Feb 2007
Location: United States
Posts: 121
Default

well it shouldn't forbid you to do it... there are several use cases where you can redirect to the same place...
__________________
Zym Framework - A Zend Framework extension library w/ demo app

SpotSec Blog:
http://spotsec.com/blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-10-2007, 10:50 PM
Junior Member
 
Join Date: Jul 2007
Posts: 5
Default

Hmm, like what? As I discovered, if you forward the router to an action in the same controller in pre-dispatch, the routing loop comes back around to the same pre-dispatch forwarding, and you've just generated a lovely infinite routing loop. If you just need to execute a method repeatedly until a condition is met, use a conditional loop instead, which beats out the rerouting loop in performance, by far.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 06-27-2008, 05:00 PM
Junior Member
 
Join Date: Jun 2008
Posts: 1
Default

Al,

I am running into, what I suspect is, the same problem. I have created an AdminController class that extendeds Zend_Controller_Action and has a preDispatch() method in it. Whenever a controller that extends this class is executed I wand the preDispatch() method to determine if the user has access to the request controller.

I am running into an endless loop as you described.

Would you be able to show/give me some more details as to how you got rid of the endless loop?

Thanks,
Gord
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 06-27-2008, 05:47 PM
Junior Member
 
Join Date: Jul 2007
Posts: 5
Default

I've altered the manner in which I handle authentication since writing this, but essentially the conditional is the same:

Code:
public function preDispatch ( )
{
    if ( !$this->LiveUser->isValid() )
    { // Check that the live user is not logged in...
        $this->_helper->FlashMessenger('You must be logged in to do that!');

        if ( $this->getRequest()->getActionName() !== 'login' )
        { // Check that you're not already redirecting to 'login'... 
            $this->_helper->Redirector->gotoRoute(array(), 'login');
            // Save yourself some trouble and make a static 'login' route...
        }

    if ( !$this->LiveUser->isAdmin() )
    { // Check that the live user is not an admin...
        $this->_helper->FlashMessenger('Admin area only!');
        $this->_helper->Redirector->goto('/');
    }
} // END preDispatch
Of course, this hinges on having an intelligent LiveUser model that can has "isValid()" and "isAdmin()" methods... But you get the picture, I hope. Lemme know.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 01:48 PM.