Welcome Guest, Not a member yet? Register   Sign In
How should this function be achieved using MVC
#1

[eluser]Lone[/eluser]
Ive got one question that has been bugging me for a while on how to properly achieve it in CodeIgniter. I have actually hit this issue a few times before for different applications but this time it is a bit easier to explain.


The Requirement
Sending of the same type of email from 3 different functions in a controller. In this email there are fields required as well.


The problem
I don't want to repeat the same email message sending process in 3 functions in the controller so what to have it in a 'common' location.


My first solution -which I don't like for having to use get_instance()
Make a library with a function that sends the email using given variables eg. name and email.

Controller
Code:
function sample_name {
  $name = "John";
  $email = "[email protected]";
  $this->load->library('misc');
  $this->misc->send_test_email($name,$email);
  $data['message'] = "Success";
  $this->load->view('controller/sample_name',$data);
}

function another_sample_name {
  $name = "John";
  $email = "[email protected]";
  $this->load->library('misc');
  $this->misc->send_test_email($name,$email);
  $data['message'] = "Success";
  $this->load->view('controller/another_sample_name',$data);
}

Library
Code:
function send_test_email($name,$email) {
  $CI =& get_instance();
  $CI->load->library('email');
  $CI->email->from('[email protected]');
  $CI->email->to($email);
  $CI->email->subject('Test Email');
  $CI->email->message('Wowzers, you just sent a test email!');
  $CI->email->send();
}


How to improve?
It is a hard one to explain, but hopefully you get what I mean, Im trying not to repeat myself in my code (DRY) and trying to see how to achieve it without having to use the get_instance() as I feel it is a bit of a last resort thing to do.


Messages In This Thread
How should this function be achieved using MVC - by El Forum - 01-24-2008, 10:52 PM
How should this function be achieved using MVC - by El Forum - 01-24-2008, 11:03 PM
How should this function be achieved using MVC - by El Forum - 01-24-2008, 11:15 PM
How should this function be achieved using MVC - by El Forum - 01-24-2008, 11:19 PM
How should this function be achieved using MVC - by El Forum - 01-24-2008, 11:39 PM
How should this function be achieved using MVC - by El Forum - 01-24-2008, 11:40 PM
How should this function be achieved using MVC - by El Forum - 01-24-2008, 11:46 PM
How should this function be achieved using MVC - by El Forum - 01-24-2008, 11:51 PM
How should this function be achieved using MVC - by El Forum - 01-24-2008, 11:59 PM
How should this function be achieved using MVC - by El Forum - 01-25-2008, 12:05 AM
How should this function be achieved using MVC - by El Forum - 01-25-2008, 12:12 AM
How should this function be achieved using MVC - by El Forum - 01-25-2008, 12:22 AM



Theme © iAndrew 2016 - Forum software by © MyBB