Before and After Actions

Friday, April 18th, 2008

Ever wanted to run some functions or create some variables before every action in a module? Or ever wanted to do something crafty after every action and before the template gets displayed?

Even if your answer is no, its good to be aware for future projects, that Symfony is hiding some very valuable methods of doing such things. Read on…

For doing stuff before every action in your module, just create a new function called preExecute() like so:

class demomoduleActions extends sfActions
{
  public function preExecute()
  {
    // ... put your stuff here that you want to run before every Action ...
  }
}

Equally for doing anything you like after every action in your module just create a new function called postExecute() like so:

class demomoduleActions extends sfActions
{
  public function postExecute()
  {
    // ... put your stuff here that you want to run after every Action ...
  }
}

It really is as simple as that. I stumbled across a little while ago whilst working on a project for a client of mine. So I thought I’d share this with you all.

Cheers,
Rob

Written by Rob | Posted on April 18th, 2008 in symfony |