Alternative to the beforeAction() event

January 11th, 2010

I often see that when someone needs something executed on every page request before anything else is executed, they usually put it in the controller beforeAction() method. Things you might want executed on every page request might be, for example (so you know what I’m talking about):

  • Handling the layout
  • Cleaning all GET and POST parameters (I don’t like to do this but some people do)
  • Language handling

I’ve seen people do this with CakePHP as well as with Yii. I find that beforeAction() is actually not an optimal place to put this sort of code. Imagine you put code for all of the examples I gave above plus three more in the beforeAction() method. You might image that the beforeAction() method would become cluttered and hard to read. I prefer lower coupling than this. We want these scripts split up into logical places. And guess what? With Yii this is easy. Application Components!

Let me write an example. Lets say we need to figure out what layout to display to the user on each request. We would like to execute some logic for this before the controller action is called. We do not use beforeFilter(). We instead create an application component. An application component is simply a class that extends CApplicationComponent.

class LayoutHandler extends CApplicationComponent {
	public function init() {
		//Put code here.

Now everything in the init() method will be executed when this application component is instantiated. So now, we just need to get it to instantiate on every request, before the action is executed. This is done in the application configuration using the preload property.

// This is the main Web application configuration. Any writable
// CWebApplication properties can be configured here.
return array(
	// The 'layoutHandler' component will pre-load
	// application components

Now there are two main things I have done here:

  1. Defined the LayoutHandler class as an application component in the components property
  2. Set the layoutHandler to preload with the preload property

You may find that this method is much more organized than simply stuffing code in CController::beforeAction(). It also leaves your code more modular.

Note that now you can even access the LayoutHandler class anywhere within your app via Yii::app()->layoutHandler. Not useful in this example, but this is often very handy.

Share and Enjoy:
  • Digg
  • Facebook
  • Google Bookmarks
  • Technorati
  • Reddit
  • RSS
  • Twitter

Categories: Yii

Tags: , Leave a comment

Comments Feed36 Comments

  1. bettor

    hi Jonah,

    I am messing around with Yii and was thinking to develop an affiliate section. Do you think extending the CApplicationComponen is a good start in terms to use it as a catch all to monitor if $_GET['affiliate_id'] has been passed to any of the pages? What are your thoughts on this?


  2. Jonah

    If you need to check $_GET['affiliate_id'] for every request, then yes, I believe an application component would be a good solution. Maybe an ‘Affiliate’ component?

  3. bettor

    yes, I am planning to develop an affiliate component and your post is very valuable and I will definitely take it into consideration. I reserve the right to ask you if I hit a bump on my way tho… :P

  4. Jonah

    Thanks :) i’ll be interested in hearing how it goes

  5. naser

    i just say tnx :-)

  6. Camac

    Nice tip, exactly what I was looking for!

  7. Matthias


  8. Walker


  9. Aurelio


  10. Shenna


  11. Glenna


  12. Cecil


  13. Roslyn


  14. Sophia


  15. Zachary


  16. Tanesha


  17. Delilah


  18. Louie


  19. Antoine


  20. Christoper


  21. Josefa


  22. Ericka


  23. Tamera


  24. Natalie


  25. Ramonita


  26. Yolanda


  27. Inez


  28. Eugene


  29. Leonard


  30. Ara


  31. Neil


  32. Tiffany


  33. Bernadette


  34. Ezra


  35. Sean


  36. Madison

    I think this is among the most important information for me.
    And i am glad reading your article. But wanna remark on few general things, The web site style is perfect, the articles is really
    great : D. Good job, cheers

Leave a comment

Feed / Alternative to the beforeAction() event