• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
When to Build your own Library ?

I'm building an order processing system - it needs to send automated emails from 5 different locations in the app. The part of me that wants to do things 'correctly' says I should be extending the email class to accept parameters from the 5 locations and to store an extended email class into a library.

I have read the user guide thoroughly on Libraries. Given I have never done this before there is a clear learning overhead. I don't really understand how best to pass parameters from multiple sources into 1 instance of 'sendmail()'

The other part of me says to heck with it. I will just place 'sendmail()' into my controllers 5 times and update the parameters in each ... easy.

What is the best practice ?? Where si the dividing line when good programmers go 'easy' or 'hard' ?

I guess if there was a great tuorial with examples that would make things easier too - as the user guide is limited.

If there are any resources I shoud look at or ideas - as ever I am Very Gratefull !

Well, basically you have couple of options;

as you already mentioned:
1) to place sendmail() in 5 places;
2) to create own library.

But you could also:
3) extend CI email class with your own;
4) create helper which will handle things for you.

Concretely I have used an e-mail helper; simply create helper which will accept different params, processed them and at the end send e-mail. Then in the 5 places in your code just call helper with the params.

I generally prefer to create / extend libraries when there is substantial code change (or new code). In my case I am working with e-mail templates, which is the reason I created email helper. E-mail template parser is library on it's own, but helper simply takes params such as template ID, receiver, subject, body and optional attachments. Then it runs it through template parser (catching possible errors) and then it calls default CI email class to send the mail.

Hope this helps a bit.


When you find yourself always repeating the same code over and over!

Thanks Guys - Smilie: how did you create Email helper ? I'm new to this and am working off the user guides which are useful, but a bit thin. I would love to see an example of how to create a helper which accepts params into the Email Class.

Many thanks Vincej

Take a look at a helper, like /system/helpers/form_helper.php and see how they do it. They key is to bring the CI superglobal into the local helper function using $CI =& get_instance();. Then you just use $CI when referencing CI instead of using $this. Like,

function email_helper($from = '')
  //make sure the FROM was set
  if ($from === '')
    show_error('You must include the FROM portion of the email.')
  $CI =& get_instance();       // Get CI
  $CI->load->library('email'); // Load the CI email library
  $CI->email->from($from);     // Set the FROM in the email
  // ...
Just have your helper function accept parameters and them pass them to CI however you need. This isn't a complete example, obviously, but should help with your question. Again, look at some of the existing helpers for better ways (like you should check to see if the function exists first...etc)

Then, to use it like in a controller or view:
$this->load->helper('email');  //load the helper
email_helper('a name');        //use the helper function and send "a name" as the FROM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.