Mike Barwick
Mike Barwick

Reputation: 5367

Routing Issue - Codeigniter

I have pages that are generated from a database, based on the URI. It functions as it should, however I can't set-up my routes to eliminate the controller and function from the URL.

$route['studios/(:any)'] = 'studios/location/$1';

Right now, I have the route to show the controller name and the URI variable (whatever that may be). However, I want to eliminate the controller name as well and just display the URI variable that's called as the URL.

Current URL would be: domain.com/studios/studio1

But I want to just display: domain.com/studio1

I tried $route['/(:any)'] = 'studios/location/$1';, but that's messing up my entire site.

What can I try next?

Upvotes: 0

Views: 75

Answers (2)

jmadsen
jmadsen

Reputation: 3675

how is it "messing up your site"?

In any case, you should not have the / before (:any)

Just:

$route['(:any)'] = 'studios/location/$1';

EDIT:

BEFORE the $route['(:any)'], you'll need to specify routes fro all your controllers; this is pretty normal, don't know if I'd call it "high maintenance", but you'll need to decide

Upvotes: 0

Kai Qing
Kai Qing

Reputation: 18843

$route['studios(/:any)*'] = 'studios/location';

This route will force everything from studios on to studios/location. You can then access any of the parameters using URI segments:

$id = $this->uri->segment(2);

If your URL was somewhere.com/studios/location/2, $id would resolve to 2

However, since you want it to just be from the root on, you will have to put your override route at the bottom of the routes file so it is assessed last:

// all other routes here. Which must be specifically 
// defined if you want a catch all like the one you mentioned
$route['(:any)'] = 'studios/location';

Alternatively, if you want a high maintenance site, you can specify a collection of routes like so:

$route['(studio1|studio2|studio3)'] = 'studios/location/$1';

Upvotes: 1

Related Questions