I'm using a Data Mapper / Gateway design pattern.
So I have a:
Mapper; Gateway; Domain Object (mainly with getters and setters); A controller; A view.
My question is: where should I instantiate the Zend Mail ? I believe the view is obviously out of question, and the gateway is, as well, not to be considered.
The controller should be kept clean, so:
Mapper our Domain Object ?
If our form will have some select box that will retrieve data from the database, then, perhaps the Mapper will be the most appropriate place to instantiate Zend Mail ?
Thank开发者_高级运维s
Hmmmm? Well with Zend you can configure your Zend_Mail in your bootstrap or by using the application.ini file or some other config file. That's how I configure mine right now. For dev, I'll write the mails to a file and for testing I'll do mail over an actual mail server.
I instantiate my Zend_Mail instance in a class that I call Mail_Service. This mail service class will create a Zend_Mail instance internally when it needs to send a mail and will use an existing Zend_Mail instance if one has been created and more mails need to be sent.
It has methods that will send predefined mails for me. For example,
Mail_Service->sendWelcomeEmail( $userInfo )
OR
Mail_Service->sendActivationEmail( $userInfo )
Say for example my controller gets a request to create a new user, then the over all flow of my code will be like this
//in the controller //process form from browser somehow UserAccountService->createNewUser( $userInfo ); ///////////////// /// Within the user account service public function createNewUser( $userInfo ) { $userMapper->createNewUser( $userInfo ); $preferencesMapper->createDefaultPreferencesForUser( $userInfo ); MailService->sendWelcomeEmail( $userInfo ); }
I don't know if this is the best way to do it, but this way my service have function names that are relevant to the service and capture a whole work flow instead of being atomic operations that just forward calls to other objects.
Hope this helps.
I always keep code that sends mail in my controllers.
Model - database/business logic
View - html / presentation layer
Controller - The code that does stuff.
精彩评论