Welcome Guest, Not a member yet? Register   Sign In
Where to put functions / classes?
#1

[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
#2

[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-gui...uting.html

In this case something like

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

Mei
#3

[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
#4

[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
#5

[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/co...tch-day-1/
#6

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




Theme © iAndrew 2016 - Forum software by © MyBB