简体   繁体   English

代码点火器路由功能

[英]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. 我想要的是: http : //example.com/category/Home-调用站点/索引函数http://example.com/category/Home/2调用参数为$ 2的站点/索引函数我在这些站点获取404错误2条规则。

What I tried was to echo the parameter of category/(:any)/(:num) and it echoed it. 我试着回显category /(:any)/(:num)的参数,然后回显它。 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. 最有趣的是,当我将类别/(:any)路由更改为站点/视图时,它正在工作,但是当我在此处设置站点/索引时,它却无法工作。 Even if I set there only site . 即使我在那里设置唯一站点。

I think what you are trying to do is to have your site class as "default controller". 我认为您正在尝试做的是将您的site类作为“默认控制器”。 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 : 我真的不知道您要如何处理您的site/view/$1site/index/$1它会像这样工作:

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/someaction将匹配$route['(:any)']并使用someaction作为字符串参数调用site控制器的view方法。

example.com/2 will match $route['(:num)'] and call the index action of the site controller with 2 as an integer parameter. example.com/2将匹配$route['(:num)']并使用2作为整数参数调用site控制器的index操作。

example.com/admin will call the index action of the admin controller example.com/admin将调用admin控制器的index操作

example.com/admin/category will call the category action of the admin controller example.com/admin/category将调用admin控制器的category操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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