Configuring Restlet 1.1 with Spring | Home | Rise of the Twitter Clones

Filed under Quick Tips on September 22, 2008 by Dave Hauenstein

Extending Zend_Controller_Action More Productively

If you’re developing web apps using the Zend Framework, you’ll notice that your application-specific controller classes will need to share similar functionality with each other. For example, you may want to see if a user is logged in before granting him access to the actions of that controller, or maybe each controller needs a few objects instantiated before it can do its business. One way to do this would be to create a file and include it in the init function. This way you only write the code once, and you can include it in every class like so:

But that’s not really so elegant. It doesn’t really support any object-oriented design patterns. It’s ugly. So what is a better solution? Use inheritance! Your controller classes don’t have to directly extend Zend_Controller_Action. Instead, write a parent class for all of your controllers that extends Zend_Controller_Action.

zend blog graph.jpg

Let’s move on with an example. Say you’re writing an application that requires all actions to authenticate. If you only had one controller, you could put this check in public function init() and it would be executed before each action is called. But this application isn’t so basic. It’s pretty complex and your architecture requires you to have multiple controller classes. So, instead, create a new class called App_Controller_Action (or namespace it however you’d like). Put it in your include path, the same way you have the Zend Framework set up: App/Controller/Action.php (this way it will autoload the same way the Zend Framework classes load).

Now all of your controller classes should be children of this class.

I’m sure you can find tons of other examples where this pattern can be useful in your applications. If you need any more help with this drop me a line at davidh@arc90.com. If you have anything to add, comment below. Have fun coding!

Post a Comment Digg Del.icio.us

Trackback Pings (TrackBack URL for this entry)

http://www.arc90.com/cgi-bin/mt4/mt-tb.cgi/192.

Comments

It seems a bit strange to call it App_Controller_Action if it is in fact a class with more than one action. It's also a bit confusing to me that a "controller extends an action" even hypotheticaly.

Perhaps Users_controller extends App_Controller_Extended or something?

Posted on September 28, 2008 8:24 AM by tomo

Zend calls their class Zend_Controller_Action. Zend_Controller is a package of classes that take care of the request and the response. More details are located on Zend's manual.

In this case, i just changed the namespace from Zend to App. App can be whatever makes sense in your application. I wouldn't change naming conventions here because it's nice to map my classes directly to the Zend classes.

Read more about Zend's naming conventions.

Posted on September 30, 2008 11:06 AM by Dave

Post a Comment:

Configuring Restlet 1.1 with Spring | Main | Rise of the Twitter Clones