Welcome Guest, Not a member yet? Register   Sign In
Code organization question - how should I organize this webservices code?
#1

[eluser]clone45[/eluser]
Hello,

I'm building a webservice for inserting leads into a database. I have a folder structure like this:

/application/controllers/webservices/leads.php

... and I can post lead information to http://www.mydomain.com/webservices/leads/add

That all works fine. The leads.php::add() function does most of the heavy lifting. I'm using Propel, and the code (highly simplified) is:

Code:
class Leads extends Webservices {

    public function add() {
         $lead_info = $this->input->post(NULL);

            $lead = new Lead();
            $lead->fromArray($lead_info);
            $lead->save();
            $lead_id = $lead->getPrimaryKey();
    }

Please ignore that there are all sorts of security issues with the above code. I've simplified it quite a bit. The actual lead inserting code is a few pages long, because it has to check for duplicate leads, yaddah yaddah.

On to my question...

I'm writing a simple lead sign-up form on the site. I was thinking to myself, "Well, it might as well use the webservice to insert the lead, since all the logic is already there.", but I don't want to POST directly to the webservice. I'd prefer that the form posts to, say, http://www.mydomain.com/signup/submit, which would be its own controller / action:

Code:
class Signup extends CI_Controller {

  public function submit {
    // Step #1: get the POSTed parameters
    // Step #2: call my webservice  (/leads/add)
  }

Of course, making an additional HTTP call doesn't sound like a good idea for performance reasons. I thought about creating a helper that contained all of the logic for adding leads. Then, both the webservice and Signup/submit action would use the helper, like so:

Code:
//  /application/controllers/signup.php

class Signup extends CI_Controller {
  public function submit {
    // call helper to insert lead
  }
}

//  /application/controllers/webservices/leads.php

class Leads extends Webservices {
    public function add() {
      // call the same helper to insert lead
    }
}

Is that a good solution? Would my helpers have access to my models, configurations, etc? Any suggestions?

Cheers,
- Bret


#2

[eluser]davidMC1982[/eluser]
If all you're concerned about is the URL then you should look here:

http://ellislab.com/codeigniter/user-gui...uting.html
#3

[eluser]clone45[/eluser]
HI David,

No, the URLs are fine. My routing works great. What I'm trying to avoid is duplication of my code, and I'm asking people if they thought that putting the code in a helper, or the model, or a library file would be the best solution. Sorry for being unclear.

Thanks,
- Bret
#4

[eluser]davidMC1982[/eluser]
OK.

Firstly, helpers are just a collection of plain old boring php functions that you can call throughout your code (but normally in your views).

Using a library would make sense if your code is not application specific and/or may be used application wide. I always think of it as code that doesn't care what my application is. I don't think this fits in your case.

In your case, I would guess you're having problems because you have code in your controllers that should be in your models. For instance, you say, "I’ve simplified it quite a bit. The actual lead inserting code is a few pages long, because it has to check for duplicate leads, yaddah yaddah." This suggests to me that you're doing a lot of work in your controller that should be done in your model and if it were done in your model, you'd have access to it across any of your controllers. You want to add a lead to your database and their are certain rules that say whether/how the rule should be added. This type of logic should be in the model.




Theme © iAndrew 2016 - Forum software by © MyBB