codeigniter中的自定义网址 [英] custom url in codeigniter
问题描述
我有控制器叫'项目'与索引的方法
I have controller called 'project' with method of index
我想传递project_id
它与这个url:
URL: http://example.com/project/index/project_id
I want to pass project_id
it works with this url:
URL: http://example.com/project/index/project_id
我想要做的是每个项目的网址
http://example.com/project_id
例如:
http: /example.com/rickyboby
what I am trying to do is to have url for each project
http://example.com/project_id
for example:
http://example.com/rickyboby
我试过这个代码
$ route ['(:any) '] =project / index / $ 1;
它的工作我可以访问project_id从url www.example.com/project_id但问题是我有其他控制器不工作,例如我有welcome控制器,url:www.example.com/welcome/是路由我到项目页面,我该如何解决呢?
I tried this code
$route['(:any)'] = "project/index/$1";
It worked I could access project_id from url www.example.com/project_id but the problem is I have other controllers are not working now, for example I have welcome controller, the url: www.example.com/welcome/ is routing me to project page, how can I solve that ?
推荐答案
您需要将所有控制器列入任何规则之前,因为它们按顺序排列, / welcome
会带您到 project / index / welcome
)。例如:
You need to whitelist all the controllers you have before the "any" rule because they go in order (so in your example, /welcome
would take you to project/index/welcome
). For example:
$route['welcome'] = "welcome";
$route['somethingElse'] = "someOtherUrl";
$route['(:any)'] = "project/index/$1";
你也可以试着用regex来做一个聪明的方法,
You can also try to think of a clever way of doing this with regex, but I think you're better off with a whitelist.
这是一个基本的编程概念,可能不属于这里,但你可以总是做如下:
This is a basic programming concept and probably doesn't belong here, but you can always do something like:
$controllers = array('welcome', 'otherController', 'etc');
foreach($controllers as $c) {
$route[$c] = $c; // make all URLs from the list go to their same-named controllers
}
$route['(:any)'] = "project/index/$1"; // handle the other URLs
不要忘记阻止用户给予与现有控制器名称匹配的项目名称(例如 welcome
),因为他们的项目将无法访问!还有另一个原因让控制器列表在某处...
One more thing - don't forget to prevent users from giving projects names that match existing controller names (such as welcome
) because their project won't be accessible! There, yet another reason to have that controller list someplace...
这篇关于codeigniter中的自定义网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!