简体   繁体   English

Codeigniter路由

[英]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. 我正在尝试编写一个从url获取参数并将其提供给默认控制器中的索引函数的应用程序。 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) 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? 理想情况下,网址看起来像example.com/(parameter),这可能吗?

Yes, you're almost there: 是的,你几乎在那里:

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

And in your index() method you fetch the parameter: 在index()方法中,您获取参数:

public function index($parameter = null){

}

$parameter will now contain anything caught by the :any shortcut, which should be equivalent to (\\w)+ IIRC $parameter现在将包含以下内容:any快捷方式,它应该等同于(\\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;: 例如,如果你有一个控制器“admin”,你的路径文件应该是:

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

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

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