Danny
Danny

Reputation: 343

Codeigniter Routing

I'm trying to write an application that takes a parameter from the url and supplies it to the index function in the default controller. Then decides what to load depending on this parameter either a default home page or a custom page.

$route['(eventguide/:any)'] = 'eventguide';

This code works but only if I have the controller in the url like so:

example.com/eventguide/(parameter)

I don't want to include the controller name. So i'm not exactly sure how to route this.

Ideally the url would look like example.com/(parameter), is this possible?

Upvotes: 1

Views: 5682

Answers (1)

Damien Pirsy
Damien Pirsy

Reputation: 25445

Yes, you're almost there:

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

And in your index() method you fetch the parameter:

public function index($parameter = null){

}

$parameter will now contain anything caught by the :any shortcut, which should be equivalent to (\w)+ IIRC

Since this is a catch-all route, be careful to put any other custom route you want before it, otherwise they will never be reached.
For ex., if you have a controller "admin", your route files should be;:

$route['admin'] = "admin";
$route['(:any)'] = "eventguide/index/$1";

Upvotes: 3

Related Questions