Organizational Research By

Surprising Reserch Topic

CodeIgniter - where to put functions / classes?


Am having problems understanding where classes should be kept in CI. I am building an application that describes / markets mobile phones.

I would like for all of my functions (i.e. getphone, getdetails etc.) to reside in one class called Mobile - I understand that this file should be called Mobile.php and reside in the controllers folder.

Can I then have multiple functions inside Mobile.php? E.g.

public function getphone() {
   xxx
   xx
   xx
}

public function getdetails() {
   xxx
   xx
   xx
}

Or do I need to put each function in its own class?

I'd really appreciate looking at some sample code that works. I've been going through the documentation and google for a few hours, and tried all sorts of variations in the URL to find a test class, but without much luck! I've even messed around with the routes and .htaccess...


asked Apr 29, 2015 in codeinither by rahulgii
0 votes
21 views



Related Hot Questions

1 Answer

0 votes

ctually it works like this:

Controllers and Models go to their perspective folders as you know it

If you want to create functions that are not methods of an object, you must create a helper file. More info here : http://codeigniter.com/user_guide/general/helpers.html

Now if you want to create your own datatypes (classes that don't extend Models and Controllers), you add them to the library folder. So if let's say you want to create a class "Car" you create this file:

class Car{

   function __construct(){}
}

and save it in the libraries folder as car.php

To create an instance of the Car class you must do the following:

$this->load->library('car');
$my_car = new Car();
answered Apr 29, 2015 by rahulgii

...