Welcome Guest, Not a member yet? Register   Sign In
Usage Question: Controller, Library, or Else?
#1

[eluser]axiom82[/eluser]
I need to execute (A) a controller-like method that (B) interacts with a model, but © is not accessible via url (as with a controller) and (D) is instead accessible via a controller method.

Below is an example:

1) A user registers an account on website via a POST form request to the user controller (a.k.a. 'user/register'). The user controller passes new user data to the user model.
2) The user model creates the new user record from the passed data. The user model creates a new email record for the user of email_type=verify (i.e. email for user account verification).
3) A cron job on a 1-minute interval sends out a batch of verification emails based on the email records with email_type=verify. As a result, the user receives the verification email with verify link.
4) The user lost the email unexpectedly. They return to the site and click the 'Resend Verification Email' button. A POST is made to the user controller's verify() method (a.k.a. 'user/verify').

Here is where it gets indecisive:

I am creating an email controller with methods for each type of email (newsletter, account verification, etc) the website sends out, a "newsletter API so to speak". A cron job will execute the email controller method associated with the specific type of email that it is scheduled to run a job for (e.g weekly email cron). The controller method gathers the necessary data from the email model (such as user data) to generate the email message and send the message via the built-in CI email library. GREAT!

Question: which is the best approach for setting up my email API: a controller or library? Since libraries are meant to be portable, I would suggest a controller. On the other hand, a controller cannot be executed from another controller, such as with the above situation where 'user/verify' needs to execute 'email/send_user_verify'. My final decision is to build an email controller that is executed via a Remote Procedure Call called within the user controller. Does this make sense?
#2

[eluser]axiom82[/eluser]
Essentially, the controller method user::verify, which is responsible for verifying user accounts via GET requests and rescheduling new verification emails via POST request, needs to get its hands on another controller's method. In this case, that is email:Confusedend_user_verify, which does the actual scheduling of verification emails. How can the two communicate?

Code:
class user{

     function verify($user_email = null, $verify_code = null){

          $this->load->model('user_model');

          if ($_SERVER['REQUEST_METHOD'] == 'GET'){

               $verified = $this->user_model->verify($user_email, $verify_code);

          }
          else if ($_SERVER['REQUEST_METHOD'] == 'POST'){

               // here is the concern...
               $this->load->library('rpc');
               $this->rpc->call('email/send_user_verify');

               // or maybe this is the solution (even though its not portable?)...
               $this->load->library('user_email'); // email library already taken/built-in
               $this->user_email->send_user_verify();

               // any ideas???

          }

          $this->load->view('user/verify', array('verified' => $verified));

     }

}
#3

[eluser]axiom82[/eluser]
I realized I was trying to over-optimize. The model itself was enough to control the flow of data.

Code:
class user{

     function verify($user_email = null, $verify_code = null){

          $this->load->model('user_model');

          if ($_SERVER['REQUEST_METHOD'] == 'GET'){

               $verified = $this->user_model->verify($user_email, $verify_code);

          }
          else if ($_SERVER['REQUEST_METHOD'] == 'POST'){

               $user = $this->user_model->user(array(
                    'email' => $_POST['email']
               ));

               $this->load->model('email_model');
               $this->email_model->subscribe_user_verify($user);

          }

          $this->load->view('user/verify', array('verified' => $verified));

     }

}

There could be a few more conditionals in there to protect against failure but this accomplishes what I needed.

Consider it resolved.




Theme © iAndrew 2016 - Forum software by © MyBB