It's been discussed before and there is public code available for it - look at what Joomla uses for an example.