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.

<?php
class LayoutHandler extends CApplicationComponent {
 
	public function init() {
		parent::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.

<?php
// This is the main Web application configuration. Any writable
// CWebApplication properties can be configured here.
return array(
	'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
 
	// The 'layoutHandler' component will pre-load
	'preload'=>array('layoutHandler'),
 
	// application components
	'components'=>array(
		'layoutHandler'=>array(
			'class'=>'path.to.LayoutHandler',
		),
	),
);

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
  • del.icio.us
  • 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?

    Cheers,
    b

  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

    VA

  8. Walker

    DH

  9. Aurelio

    PD

  10. Shenna

    BB

  11. Glenna

    XW

  12. Cecil

    ZF

  13. Roslyn

    SX

  14. Sophia

    WV

  15. Zachary

    WP

  16. Tanesha

    IK

  17. Delilah

    DU

  18. Louie

    IS

  19. Antoine

    YC

  20. Christoper

    UZ

  21. Josefa

    DM

  22. Ericka

    KK

  23. Tamera

    TL

  24. Natalie

    CY

  25. Ramonita

    QI

  26. Yolanda

    PA

  27. Inez

    OF

  28. Eugene

    GC

  29. Leonard

    XY

  30. Ara

    OW

  31. Neil

    BY

  32. Tiffany

    ER

  33. Bernadette

    JA

  34. Ezra

    WK

  35. Sean

    II

  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

http://php-thoughts.cubedwater.com / Alternative to the beforeAction() event