views:

496

answers:

3

I'm new to CI and URI routing in general.

I created a new app. Set the default controller to Main. In Main, I have an index method, a popular method and a recent method.

When I load my app, the url shows up as http://localhost/myapp... this obviously loads up the index method in the Main controller... that's fine.

Now how do I route my URIs so I can load up the popular and recent method by going to http://localhost/myapp/popular and http://localhost/myapp/recent respectively?

A: 

If popular and recent are actual pages in your application, as opposed to functions, you ought to move those to their own controllers, as opposed to keeping them under main.

Gavin Miller
I'm not too familiar with CodeIgniter specifically, but that doesn't sound right at all. I'm 99% sure it's router can do this, and messing with the already configured `mod_rewrite` is probably not a good idea, nor is it consistent.
Mark
Thanks Mark I wasn't aware of the router in CI.
Gavin Miller
+1  A: 

You can use CodeIgniter's routing features. To do that, just add the following lines to your application/config/routes.php file:

$route['recent'] = "main/recent";
$route['popular'] = "main/popular";
Franz
You cannot have a controller with the name, "index". It is reserved. See http://codeigniter.com/user_guide/general/reserved_names.html
Thorpe Obazee
Oops. Yeah, fixed the typo. Index was supposed to be main.
Franz
no problem. It happens :)
Thorpe Obazee
+1  A: 
$route['recent'] = "your_controller/recent";
$route['popular'] = "your_controller/popular";

That's what you will need. Any call to "recent" will route to "your_controller/recent". The same goes with popular.

Thorpe Obazee