Filter vs. behavior

Hi,

I’m new to Yii having previously had experience in using CakePHP and deciding to make the jump across. Still getting my head around a few things so some help would be greatly appreciated.

I currently have some code working as a prefilter, attached to a base controller that all my other controllers extend. It checks the domain and first few parts of the URL against the database for information (like organisation and site). I thought a prefilter would be a good approach but I’m not entirely sure.

Ideally I’m looking for:

  • Code runs before any controller executes
  • Sets data (org and site) that is needed for any subsequent code (e.g. Page controller / model would limit results to that org + site).
  • Correctly throws 404’s when it doesn’t find matches.

Just can’t see how I should be passing the data around to the rest of the app and want to check if a prefilter is the ideal way.

Thanks,

Dave

Why not create a component which handles only this type of actions ?

Then, "preload" the component in your application config.

The init() method of the component will be called right when the application starts, before the onBeginRequest event handler , so you can do your math there and populate the component properties with the values you need, then, during runtime, you can have calls like Yii::app()->component->getOrganization() which returns the organization or getSite() to return the site…etc.

I think I get the idea. That sounds like a good way to encapsulate things and allows it to be accessible everywhere :)

Thanks! Still finding my way around this framework and it’s always difficult finding out the better ways of doing things.

You tell me about it :D