Hi I'm currently working on an 'handler' framework for cake vendor dir :) It's php5 only though, but it supports a few different nice things - the real simple outline is: You have a 'Handler' (singleton) where you can 'trigger' some default events and your own custom ones For each Handler, you have the option to attach any amount of observers, that depending on the event you trigger, will be notified with relevant data. You also have the option to attach 'workers' - that could be a FileLoggerWorker, or XmlLoggerWorker - your observers can use as a resource.
A really simple example of use would be something like this in my bootstrap file: <code> vendor( 'EasyHandler/bootstrap' ); EasyHandlerConfig::getInstance()->addRoot( APP . 'EasyHandler' ); /* @var $phpErrorHandler PhpErrorHandler */ $phpErrorHandler = EasyHandlerFactory::getHandler('PhpError'); $phpErrorHandler->writeEvent(EH_MAIL, E_WARNING, true); $phpErrorHandler->writeEvent(EH_LOG, E_STRICT, false); $phpErrorHandler->loadWorker('File',array('filename' => 'php_errors.txt')); $phpErrorHandler->loadObserver('PhpError'); $phpErrorHandler->loadObserver('PhpErrorMail',array('email' => '[EMAIL PROTECTED]')); set_error_handler('cakeErrorHandler'); /** * Wrapper to make sure that the PHP errors is handled by our class * * @param integer $log_level * @param string $log_text * @param string $error_file * @param integer $error_line */ function cakeErrorHandler( $log_level, $log_text, $error_file, $error_line ) { PhpErrorHandler::getInstance()->handleError( $log_level, $log_text, $error_file, $error_line ); } </code> What it does should be rather simple to figure out, but in short : a) vendor the boostrap for the framework b) add a new search path ( APP . 'Easyhandler' ) - this is where the factory will look for observers, workers, handler, exceptions ect. c) Request the PhpErrorHandler from the factory (handles auto-loading of interfaces ect. ) d) Enable two events ( EH_MAIL = mail event, and EH_LOG is logging event ) e) Attach a FileWorker that is going to output to php_errors.txt ( default log path is in LOGS ) f) Load a PhpErrorObserver that is going to handle the error level filtering ect. g) Load a PhpErrorMailObserver that will send me a nice email each time there is a E_WARNING triggered somewhere h) Change the default PHP error handler - so it uses my EasyHandler framework A few notes on this is that the PhpErrorHandler auto-enables logging for the current level of error_reporting() you have, that's why they are omitted. Also, if you prefer the error log to be in XML, just replace the line with: <code> $phpErrorHandler->loadWorker('Xml',array('filename' => 'php_errors.xml')); </code> That only requires the worker to implement the XmlLogger interface :) Other examples could be in my AppController: <code> this->log = EasyHandlerFactory::getHandler('Logger'); $this->cache = EasyHandlerFactory::getHandler('Cache'); $this->cache->loadObserver('CacheApc'); $this->benchmark = EasyHandlerFactory::getHandler('Benchmark'); $this->Event = EasyHandlerFactory::getHandler('CakeEvent'); $this->Event->addObserver( EasyHandlerFactory::getObserver('CakeSecurity') ); $this->Event->addObserver( EasyHandlerFactory::getObserver('CakeMenu') ); </code> Where one of the more interesting ones is Benchmark and Cache handler ;) The Event handler is used to 'overtake' everything that is possible to overtake from cake in the form of callbacks. <code> Function afterFilter() { $this->Event->triggerControllerEvent( $this, __FUNCTION__); } </code> Is going to trigger all observers in CakeEventHandler that implements the CakeControllerInterface :) The same is doable for models aswell /Jippi -----Original Message----- From: cake-php@googlegroups.com [mailto:[EMAIL PROTECTED] On Behalf Of ianh Sent: 2. februar 2007 09:42 To: Cake PHP Subject: Re: Email Notification Hi All, Fully agree that the way to use model callbacks for things like sending emails is to use observers - there is a tutorial on the Bakery on this. It is such a flexible way of doing things that I have started to put quite a lot of business logic that might otherwise go elsewhere into observers. I have found that I can log errors, access any model easily (getModel method) and trigger emails etc without introducing dependencies into my models and allowing my controllers to focus on handling application flow and exceptions. Ian On 1 Feb, 22:25, Langdon Stevenson <[EMAIL PROTECTED]> wrote: > Hi Tarique > > >> What is the recommended approach for an AfterSave function triggering > >> and sending an email notification on a saved or submitted form? Thanks. > > > I personally prefer doing this in the Controller as I tend to use a > > component to send emails. Keeps the Model free of any dependencies > > > If at all you want to do this with the Model then I would suggest make > > the Model Observable and the Email component should be an Observer > > I was having thoughts along those lines too. I have a project underway > that will require exactly that functionality. > > For instance: when a stock level in the database reaches a certain point > it should trigger a variety of functions like emailing people and > queuing new orders. I don't want to tie this to the model, but make it > a configurable option with its own controller. > > Regards, > Langdon --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~----------~----~----~----~------~----~------~--~---