简体   繁体   中英

codeigniter routing functions

I have a small problem with routing.

My routes:

$route['category/(:any)/(:num)'] = "site/index/$2"; //not working
$route['category/(:any)'] = "site/index"; //not working
$route['category/(:any)/(:any)'] = "site/view/$2"; // working
$route['Search'] = "site/search"; // working

What I want: http://example.com/category/Home - call site/index function http://example.com/category/Home/2 call site/index function with parameter $2 I'm geting 404 erro at those 2 rules.

What I tried was to echo the parameter of category/(:any)/(:num) and it echoed it. This echo was inside the index function. The views adn models exists in the paths I declared. The index page itself wouldn't work without it. So I think the problem has to be in routing

The most interesting thing is that when I change the category/(:any) route to site/view it is working but when I set there site/index it is not working. Even if I set there only site .

I think what you are trying to do is to have your site class as "default controller". Just try this :

$route['default_controller'] = "site";
$route['(:any)'] = "site/view/$1";
$route['(:num)'] = "site/index/$1";

I don't really know what you are trying to do with your site/view/$1 and site/index/$1 it will work like this :

example.com/someaction will match $route['(:any)'] and will call the view method of the site controller with someaction as a string parameter.

example.com/2 will match $route['(:num)'] and call the index action of the site controller with 2 as an integer parameter.

example.com/admin will call the index action of the admin controller

example.com/admin/category will call the category action of the admin controller

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