Extending the default module action

Often in a project, module actions have to manage the same common kind of functionalities :

  • paging
  • sorting
  • filtering
  • any other general job

You end up with actions that implements each functionality, maybe into separate methods :

class articleActions extends sfActions
  protected function processSort() { …  }

  protected function processFilter() { …  }

  public function executeIndex()
     $this->pager = new sfPropelPager(‘Article‘, 20);

Let’s extract these jobs and use them in all your modules.

Extending sfActions

A Symfony action is a stack of extended classes, so you can easily add your own class to the stack and can put all the common code in it.

Let’s factorize all theses functionalities into a custom action class that extends sfActions.

Let’s create the /lib/myBaseActions.class.php file :

class myBaseActions extends sfActions
  // Implement your general methods here
  protected function processSort()
  protected function processFilter()

Now, simply change the parent class in your module action class :

class articleActions extends myBaseActions
  public function executeIndex()

The dirty job is now centralized and executed implicitly in all your modules.

Besides, extracting the logic that way forces you to write decoupled and cohesive code, as each method only do a specific job.

About auto-generated modules

You should take a look at how a module action generated by the sfAdminGenerator.

Check /cache/prod/modules/autoArticle_admin/actions/actions.class.php.

You can get inspired from it, specially from these design principles :

  • sorting and filtering infos are stored in the session
  • Criteria modifications for both is split into separate methods

  1. Andre

    Hi there,

    thank you for the description. I have to add that it is necessary to do a clear-cache on the project. This took me about two hours.


  1. 1 rpsblog.com » A week of symfony #86 (18->24 august 2008)

    […] Extending the default module action […]

  2. 2 Recent Links Tagged With "paging" - JabberTags

    […] public links >> paging Extending the default module action Saved by obibynproductions on Mon 22-12-2008 Link Listing – August 20, 2008 Saved by ShadowlyZach […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: