Page 1 of 1
Best way to abstract controller methods in ZF
Posted: Thu Apr 09, 2009 3:59 pm
by Theory?
So I have my main controllers all set up, but the action for the nav and some other modules belong solely to the layout alone. So I have this little snippet at the beginning of the actions that checks to see the source of the request and ensure that it's an action request and not a direct request.
Code: Select all
$request = $this->getRequest();
if ($request->getParam('module') == $request->getModuleName()
&& $request->getParam('controller') == $request->getControllerName()
&& $request->getParam('action') == $request->getActionName()) {
// direct call to an actionStack-only action.
$this->_helper->redirector->gotoRouteAndExit(array(), null, true);
}
Now this is all fine and dandy, but it seems like this snippet should show up enough that I can find a better way to bring this into my other controllers. I know the view has view helpers that are automatically loaded when the viewRenderer is called, but is there something I can maybe introduce into the actionStack or just preload it somewhere else?
Re: Best way to abstract controller methods in ZF
Posted: Thu Apr 09, 2009 8:30 pm
by josh
I would look into Front Controller plugins. You create a plugin class that overrides a method that corresponds to an event, in your case your plugin would most likely implement a preDispatch() method ( event handler ) that performed that behavior. I don't get what your plugin actually does though, what do you mean by direct request ??
Edit: ah nvm, I see "actionstack only", makes sense.
Re: Best way to abstract controller methods in ZF
Posted: Sat Apr 11, 2009 5:17 pm
by Theory?
josh wrote:I would look into Front Controller plugins. You create a plugin class that overrides a method that corresponds to an event, in your case your plugin would most likely implement a preDispatch() method ( event handler ) that performed that behavior. I don't get what your plugin actually does though, what do you mean by direct request ??
Edit: ah nvm, I see "actionstack only", makes sense.
I think I get what you mean. I provide the method to the FC so at dispatch the function becomes available to every controller and then I can just call it at will....right?
I want to do this, really:
Code: Select all
public function fooAction() {
if(!$this->actionStackOnly()) { ...//do some forwarding here } else { ..//execute function as normal }
}
Re: Best way to abstract controller methods in ZF
Posted: Sun Apr 12, 2009 8:23 pm
by josh
That's one way to do it, but that would create duplicate code
Re: Best way to abstract controller methods in ZF
Posted: Mon Apr 13, 2009 1:58 pm
by Theory?
Ok, but if I put a method right before the dispatch then every method would receive the check and that would break my entire app because not every action is a stack request. How could I tell it to validate only the methods I need it to?
Re: Best way to abstract controller methods in ZF
Posted: Tue Apr 14, 2009 2:18 pm
by josh
Configure an ACL object and inject it into your plugin's constructor
Re: Best way to abstract controller methods in ZF
Posted: Tue Apr 14, 2009 2:23 pm
by Theory?
josh wrote:Configure an ACL object and inject it into your plugin's constructor
Sweet! Thanks so much!
Re: Best way to abstract controller methods in ZF
Posted: Tue Apr 14, 2009 7:06 pm
by josh
Something like this. You could have 2 ACLs & plugins, 1 for user access and a separate one for blocking out "protected" actions.
http://devzone.zend.com/article/3510-Ze ... vanced-Use