简体   繁体   English

CodeIgniter:简单的URL路由问题

[英]CodeIgniter: Simple URL routing question

I'm new to CI and URI routing in general. 我一般都是CI和URI路由的新手。

I created a new app. 我创建了一个新的应用程序 Set the default controller to Main . 将默认控制器设置为Main In Main , I have an index method, a popular method and a recent method. Main ,我有一个index方法,一个popular方法和一个recent方法。

When I load my app, the url shows up as http://localhost/myapp ... this obviously loads up the index method in the Main controller... that's fine. 当我加载我的应用程序时,该URL显示为http://localhost/myapp ...这显然加载了Main控制器中的index方法...这没关系。

Now how do I route my URIs so I can load up the popular and recent method by going to http://localhost/myapp/popular and http://localhost/myapp/recent respectively? 现在我如何路由我的URI,以便我可以分别转到http://localhost/myapp/popularhttp://localhost/myapp/recent来加载流行的和最近的方法?

You can use CodeIgniter's routing features. 您可以使用CodeIgniter的路由功能。 To do that, just add the following lines to your application/config/routes.php file: 为此,只需将以下行添加到application / config / routes.php文件中:

$route['recent'] = "main/recent";
$route['popular'] = "main/popular";
$route['recent'] = "your_controller/recent";
$route['popular'] = "your_controller/popular";

That's what you will need. 这就是你需要的。 Any call to "recent" will route to "your_controller/recent". 对“recent”的任何调用都将路由到“your_controller / recent”。 The same goes with popular. 流行也一样。

如果popularrecent是应用程序中的实际页面,而不是函数,则应将它们移动到自己的控制器,而不是将它们保持在主要状态。

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

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