CodeIgniter Forums
Where to put functions / classes? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Where to put functions / classes? (/showthread.php?tid=48889)



Where to put functions / classes? - El Forum - 02-01-2012

[eluser]dharmy[/eluser]
Hi all

Am having problems understanding where classes should be kept in CI. I am building an application that 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.

Code:
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...

All I am trying to achieve is the following:

Code:
http://www.site.com/model/HTC-Desire/
to be re-routed to a function that accepts HTC-Desire as a parameter (as I need it for a DB lookup). The default controller (for my home page) works fine, but can't get anything to work thereafter.

Any ideas?

Thanks


Where to put functions / classes? - El Forum - 02-01-2012

[eluser]meigwilym[/eluser]
If you want to achieve that kind of structure then you need to set a few routes http://ellislab.com/codeigniter/user-guide/general/routing.html

In this case something like

Code:
$route['model/(:any)'] = "mobile/getphone/$1";

Mei


Where to put functions / classes? - El Forum - 02-01-2012

[eluser]dharmy[/eluser]
Thanks - so would I have function getphone in the mobile.php file (in the controllers folder)?

Also I presume the $1 refers to the parameter that getphone will accept?

Thanks


Where to put functions / classes? - El Forum - 02-01-2012

[eluser]meigwilym[/eluser]
Quote:so would I have function getphone in the mobile.php file (in the controllers folder)?

Yes (but as it's a class call them 'methods' to save any misunderstandings).

Quote:Also I presume the $1 refers to the parameter that getphone will accept?

Yup. You should have

Code:
public function getphone($phone_name) {
// code here
}

In the above case $phone_name would be HTC-Desire.

Mei


Where to put functions / classes? - El Forum - 02-01-2012

[eluser]Jason Stanley[/eluser]
You are coming across as an ultra-noob (no offense!)

This may be a good place to start with CI if you are new to it.

http://net.tutsplus.com/articles/news/codeigniter-from-scratch-day-1/


Where to put functions / classes? - El Forum - 02-01-2012

[eluser]vbsaltydog[/eluser]
watch video tutorials
read the user guide
experiment
reverse engineer