1

I'm new to MVC, being RESTful, and CodeIgniter. I'm trying to get into them in my spare time, so this is largely an academic question. I'm trying to build a URL that will display the availability of a particular hotel room, for a particular hotel. I figured the RESTful way to do this would be the following:

http://url/Hotel/2/RoomAvailability/3/
  • "Hotel" is the controller
  • "2" is the hotel ID
  • "RoomAvailability" is the Method
  • "3" is the Room ID

How would I set up my controller in codeigniter to handle this? Currently I'm thinking I could do either of the following:

  • Do something with mod_rewrite to redirect to the RoomAvailability() method
  • Do something with the index() method and redirect to the RoomAvailability() method

Really this is a pretty generic question, as I just want to be able to do the following:

http://url/model/method-argument/method-name/more-method-arguments

I'm honestly having a hard time coming up with search terms to find out what to use (other than RESTful and CodeIgniter, which havent been too helpful0.

I'm really just looking for guidance; not for someone to write my controller for me. Also, if this URL that I'm going for is horrible, and not RESTful at all; please feel free to point out a better way.

tereško
  • 58,060
  • 25
  • 98
  • 150
Allen Rice
  • 19,068
  • 14
  • 83
  • 115

2 Answers2

3

What about this url set up:

http://url/hotel/method/hotel_id/room_id

Then you could do something like this:

class Hotel extends Controller {

 function RoomAvailability() {
   $hotel = url_segment(3);
   $room = url_segment(4);
   do_magic();
 }

}
Jayrox
  • 4,335
  • 4
  • 40
  • 43
  • I thought about that, but is this considered restful? I thought to be considered restful you would have to have something in the order I had it. As in "a Hotel's (this hotel id) Room's (this room id) availability". I may be completely wrong though. – Allen Rice Feb 20 '09 at 19:11
  • well in that case, using the routes is probably the best way. routes are very easy to set up in codeigniter. $route["hotel/:num/RoomAvailability/:num"] = 'RoomAvailabilityClass'; might get you going – Jayrox Feb 20 '09 at 19:45
  • Changed the accepted answer, that route is EXACTLY what I needed, it works great now, thanks! – Allen Rice Feb 20 '09 at 20:04
2

Checkout the CI User Guide, specifically the part on routing.

https://www.codeigniter.com/user_guide/general/routing.html

Stack Programmer
  • 679
  • 6
  • 18
davethegr8
  • 11,323
  • 5
  • 36
  • 61