Where to put an email function |
[eluser]CARP[/eluser]
Hi guys I have built an app for government that allow users to request a new e-mail address account related to government. This CI app sends mail notifications to mail server admins when a new user requests an account. I have this code cloned in some controllers Code: $this->load->library('email'); I'd like to build a function somewhere with some parameters for sending mails, for example: Code: send_email($to, $from, $cc, $bcc, $message); Where should I build this function? Inside a controller? How should then I call it from another or the same controller? Thanks a lot in advance. Hope you've understood my question.
[eluser]Sarre[/eluser]
How about inside the library itself? Then from the controller you can call it like this: $this->email->send_email($to, $from, $cc, $bcc, $message) [Edit] woops, off course the e-mail library is a CI-native library, i had forgotten that. Still you can extend that library See libraries (> extending native libraries)
[eluser]CARP[/eluser]
Thanks Sarre for the tip After reading that topic in the user_guide, got other question Could I call & use the std email library from my own library? I think that rather than extending the email library, I'll build a "common_functions" library with some snippets commonly used... Thanks again
[eluser]sophistry[/eluser]
depending how you want to set it up you could also make a master controller (a front-controller) by subclassing all of your controllers to one master controller then put the snippet functions in there. also, a helper is a good place for code that simply "wraps" other code. read up on helper in the guide. if you want to still build your own separate library that talks to CI the user guide referenced above has info on that too. search on get_instance.
[eluser]CARP[/eluser]
Aha... cool I just wanted to know the best way to build a "snippet" code repository I could use and call frequently from my controllers The "subclassing controllers" concept is too far from my knowledge. I think I'll go by building a helper or library to put my common used functions Thanks again
[eluser]sophistry[/eluser]
found this code by wiredesignz that illustrates multiple inheritance in CI controllers nicely. see Quote:the top post on this page of this enlightening OO-design oriented thread from a few months back.
[eluser]evanwalsh[/eluser]
I would put something like that in a "common" model. That's how I do things, but I'm not saying it's the best.
[eluser]sophistry[/eluser]
@evanwalsh - yes, that would work too - you can put code pretty much anywhere in CI! ;-) but, devs tend to think of models in terms of a shell around a database. since the requirement is to send email that kind of breaks down the organization. it would work, but putting non-db oriented code in a model is kind of like hiding it.
[eluser]CARP[/eluser]
@sophistry you're right. I've finally built a helper, and it is working great Thanks 2 everyone for helping
[eluser]Gabriel Jones[/eluser]
@CARP - Sounds to me you would like to override the send_email helper function. In which case you may want to create a file called MY_email_helper.php and create the override there. Where 'MY_' is your subclass prefix you define in your config. I have a similar situation, and after reviewing php.net I found it to be fairly straight forward. |
Welcome Guest, Not a member yet? Register Sign In |