简体   繁体   中英

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?

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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM