|
|||
|
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:
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
Code:
admin/index Code:
admin/login 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);
Code:
echo "Pre Dispatch, "; Guess which one works? Hint: it's the short one. Anybody got a clue? |
|
|||
|
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...!
|
|
||||
|
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 |
|
|||
|
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.
|
|
|||
|
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 |
|
|||
|
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
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|