Reputation: 8108
Codeigniter models are generally tied specifically to a database table (or tables).
We are finding that there are some objects that we use that have member variables and methods, but do not relate to a specific table in our database.
An example of this sort of object is the OSMAddress
return from the OpenStreetMap web service. This has fields (depending on the location) of:
this lends itself naturally for being passed around, so we have created a PHP class to encapsulate it.
The trouble is, we are not sure where this class should live. The operation to get the information is a CodeIgniter helper
function, but OSMAddress
is not a helper.
We are using the standard CodeIgniter structure:
So:
Upvotes: 1
Views: 331
Reputation: 8964
If these classes extend CI_Model
then they should probably be in the models directory.
If they don't extend anything - in other words, they are a custom class
- then they probably belong in the libraries directory. They can be called "libraries".
That said, "Models" need not be related only to tables and/or database methods. Many devs regard them as the place for "business logic" or for any manipulation of data even if no database is involved.
There's no real right or wrong. It's a matter of what "separation of concerns" makes the most sense to you and your team.
One more thing. If the file does not define a class
then it's a "helper".
Upvotes: 1
Reputation: 1686
Helpers can only have a bunch of globally accessed reusable functions/methods that are used for specific purpose. Here you can't define any class.
I think you meant libraries, which will have classes with methods like in java. These libraries can then be used in your controller classes as per your need.
For eg: In your case, you are trying to get OSMAddress
from a web service.
You need to create a PHP file under library folder to define a class.
application/library/your-file-name.php
Define a class OSMaddress
class OSMaddress{
public function __construct() {
//define all your variables here
}
//define all your web service methods here
public function function_1(){
//your code
}
}
Once you are completed, load your library in your controller class
class Address extends CI_Controller{
public function __construct() {
parent::__construct();
$this->load->library('OSMaddress');
}
public function function-name(){
//Now call your function from the library
$data = $this->OSMaddress->function_1();
//function_1 will return the data
}
}
For more documentation, you can refer the docs
Hope this can help you.
Upvotes: 3