CodeIgniter Forums

Full Version: Route to e.g. MyController, if controller from URI not found
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
I've been given an existing site (not using CI), with (most) page data stored in a DB, and all incoming requests that don't match an existing file are rerouted to the front controller (index.php)

The redirect looks like this:

Code:
RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule .$ index.php [NC,L]

From there, index.php grabs the URI and splits it on slashes, compares the segments to fields in the DB, and returns the appropriate page content.

We're adding some significant new functionality, and I'd like to convert the backend to CI.

Since there is a TON of page data, already structured, in the DB (and most of it is "static" content), I'd really like to keep that in place and not include every faux-directory and faux-page as a controller/method, especially since all those methods would basically do the same thing.

My thought was to turn that existing front controller into a CI controller, and route all requests to non-existing controllers to that one (I'll call it "legacy"). If the new functionality has a dozen or so "real" controllers (standard CI approach), any requests with a first-position segment that does not match any of those real controllers should be routed to the "legacy" controller.

For example, say the new functionality (that I'd be implementing) has a Products controller, a Services controller, and an Events controller - so example.com/products/, example.com/events/, and example.com/services/* would all behave 'normally', but anything else would go to the legacy controller.

I believe I could use routes to list all the "valid" controllers, then send anything else to the legacy controller, but I'd like to avoid having to list them explicitly (in case I - or anyone else - ever added or changed a controller, I/they shouldn't need to update the routes file).

I also considered mucking with the error page, but that's not an attractive option either.

Any ideas?

TYIA

El Forum

[eluser]Unknown[/eluser]
looks like this is the best option:
Code:
$route['404_override'] = 'legacy';

EDIT: actually, doesn't look like this'll allow me to keep the URI segments... if this is a viable option, is there a way to pass the original URI (or segments) to the 404_override?

EDIT: data still exists in $_SERVER['REQUEST_URI'] - i think this is solved (although feels a bit hacky)