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:
public function init()
{
require_once ‘sharedFunctions.php’;
}
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.
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).
class App_Controller_Action extends Zend_Controller_Action
{
public function isAuthenticated()
{
...
}
public function moreSharedFunctionality()
{
...
}
Now all of your controller classes should be children of this class.
class Users_Controller extends App_Controller_Action
{
public function init()
{
if($this->isAuthenticated())
{
...
}
$this->moreSharedFunctionality();
}
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!
tomo said:
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?
Dave said:
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.
Rocco said:
it might be noteworthy that App/Controller/Action.php has to go into the /library folder, so that App is at the same level as Zend in order to work – maybe not the best way to separate lib and app.
to keep App/… within the application folder and enable the Zend autoloader to find your files, add this to your bootstrap:
set_include_path(APPLICATION_PATH.’/../application’.PATH_SEPARATOR.get_include_path());
Tyler Gaw said:
i’m pretty damned late on the uptake here, but I just wanted to point out that I used this exact same method on my new site. It’s very, very helpful, and lets you get the business end of your site done so you can concentrate on more fun things.
ronal said:
Yo veo la explicacion muy clara y simple
greetings
Linkamp said:
Thanks great and simple example