CodeIgniter:简单的 URL 路由问题
一般来说,我对 CI 和 URI 路由很陌生。
我创建了一个新应用程序。将默认控制器设置为Main
。在 Main
中,我有一个 index
方法、一个 popular
方法和一个 recent
方法。
当我加载我的应用程序时,网址显示为 http://localhost/myapp
...这显然会加载 Main
中的 index
方法代码>控制器...没关系。
现在,我如何路由我的 URI,以便可以通过访问 http://localhost/myapp/popular
和 http://localhost/myapp/recent< 来加载流行的和最近的方法/代码> 分别?
I'm new to CI and URI routing in general.
I created a new app. Set the default controller to Main
. In Main
, I have an index
method, a popular
method and a recent
method.
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 CodeIgniter 的路由功能。为此,只需将以下行添加到您的 application/config/routes.php 文件中:
You can use CodeIgniter's routing features. To do that, just add the following lines to your application/config/routes.php file:
这就是您所需要的。任何对“recent”的调用都将路由到“your_controller/recent”。流行也是如此。
That's what you will need. Any call to "recent" will route to "your_controller/recent". The same goes with popular.
如果
popular
和recent
是应用程序中的实际页面(而不是函数),那么您应该将它们移至它们自己的控制器,而不是将它们保留在 main 下。If
popular
andrecent
are actual pages in your application, as opposed to functions, you ought to move those to their own controllers, as opposed to keeping them under main.