views:

811

answers:

1

How can push a URI like this in CodeIgniter (1.7.1 currently):

example.com/seg1/seg2/seg3/seg4/
or
example.com/seg1/seg2/
etc

..through a single class method in a controller whose name does not appear in the URI? In a regular PHP scenario I would use mod_rewrite something like this:

RewriteRule ^([^/]+)/$ myfile.php?one=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/$ myfile.php?one=$1&two=$2 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ myfile.php?one=$1&two=$2&three=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ myfile.php?one=$1&two=$2&three=$3&four=$4 [L]

(I sanitize and validate the segments extensively in PHP, returning 404 if invalid)

...but in Codeigniter I can't figure out how to do this without hard coding at least the first segment, but then using custom routes it still wants to treat the subsequent segments as method calls.

I'm a newbie to CI, but have so far managed to port over the entire existing site with the exception of this part of it. I don't see how all the parts come together on this problem, so any suggestion is welcome.

Clarification: I read through documentation on URI library, routing etc before starting my project, and they were helpful for various things, but this specific problem is not addressed by any of them. I'm not seeing how all segments of the URI can essentially be funneled through a controller that is not named in the URI and where all segments are arbitrary. The routing examples assume you know the value of the first segment. I already know how to remove "index.php" as well as access segments.

+1  A: 

Have you looked up URI routing in the user guide and the wiki? They should tell you almost anything about routing, rewriting and accessing the different URI segments.

http://codeigniter.com/user_guide/general/routing.html

http://codeigniter.com/user_guide/libraries/uri.html

http://codeigniter.com/wiki/mod_rewrite/

[Edit:]

Here's the long description: There is no way to not "hard-code" the first segment, and you can still "hard-code" the second segment.

What you want to accomplish can be nearly done by editing the routes in system/application/config/routes.php:

$route['(:any)'] = 'your_default_controller/index/$1';

$route['default_controller'] = "your_default_controller";

So, the first segment of your URI will be the method of the controller. You can access all segments of your (initial) URI by

$this->uri->segment(n)

You would then use the method index to call the desired function for each request.

On a side note: Why do you want to use an MVC framework for that, as you do not use much of the benefits of MVC?

Residuum
After removing index.php you may use $this->uri->segment(n).. I think Residuum gave the correct answer but you haven't realized it yet :)
Ahmet Kakıcı
Thanks Residuum. I'm doing this is because I'm porting over an existing site, part of which is a link directory dynamically constructed from a db to reduce hundreds of directories to one file. So what you suggested is the first thing I tried, but what I missed was that to access seg2 from: example.com/seg1/seg2 and using the following route: $route['(:any)'] = 'a_controller/index/$1' would be $this->uri->segment(4) and not $this->uri->segment(2) which kept returning "index". I was expecting to access them as I saw them in the URI. Stupid newbie mistake, thanks for setting me straight
Fo
..and I was using rsegment instead of segment :p
Fo