Best way to abstract controller methods in ZF

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Theory?
Forum Contributor
Posts: 138
Joined: Wed Apr 11, 2007 10:43 am

Best way to abstract controller methods in ZF

Post 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?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Best way to abstract controller methods in ZF

Post 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.
Theory?
Forum Contributor
Posts: 138
Joined: Wed Apr 11, 2007 10:43 am

Re: Best way to abstract controller methods in ZF

Post 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 }
}
 
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Best way to abstract controller methods in ZF

Post by josh »

That's one way to do it, but that would create duplicate code
Theory?
Forum Contributor
Posts: 138
Joined: Wed Apr 11, 2007 10:43 am

Re: Best way to abstract controller methods in ZF

Post 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?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Best way to abstract controller methods in ZF

Post by josh »

Configure an ACL object and inject it into your plugin's constructor
Theory?
Forum Contributor
Posts: 138
Joined: Wed Apr 11, 2007 10:43 am

Re: Best way to abstract controller methods in ZF

Post by Theory? »

josh wrote:Configure an ACL object and inject it into your plugin's constructor
Sweet! Thanks so much!
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Best way to abstract controller methods in ZF

Post 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
Post Reply