4

这是我可以问这个问题的最简单的方法,因为我还没有完全理解发生了什么,或者我做错了什么。

我的网址有问题。

http://localhost/index.php/user 相同 http://localhost/ http://localhost/index.php/user/something 不同 http://localhost/something

我怎么做http://localhost/something

它必须是http://localhost/user/something,我如何使它工作?

4

4 回答 4

2

您需要了解 CodeIgniter 的 URL 是如何工作的。

  • URL 由一些段组成。http://localhost/index.php/user/something/thing在本例user中,somethingthing是 URL 的片段。

  • URL 的段指示将运行哪个控制器以及该控制器的哪个方法。http://localhost/index.php/user/something/thing在此示例中,something来自user控制器的方法被调用并thing作为参数传递给该方法。

  • URL 的第一段表示控制器。

  • URL 的第二部分表示该控制器的方法。

  • 以下段作为参数发送到该方法。

但是有一些默认值。

  • 如果您的 URL 是http://localhost/index.php/something,则您已something指定为控制器,但由于您未指定任何方法,因此index调用的是默认方法。所以上面的网址是一样的http://localhost/index.php/something/index

  • 如果您的 URL 是http://localhost/index.php/,则您没有指定任何段(没有控制器和方法)。因此,其中指定的默认控制器是application\config\routes.php加载的控制器。该控制器的哪个方法将被调用?当然是index方法。

    --您可以通过更改文件$route['default_controller'] = "site";中适合您的应用程序的内容来设置默认控制器application\config\routes.php

  • 如果您想http://localhost/user/something与 相同http://localhost/index.php/user/something,则必须为您的应用程序创建自定义路由。更多信息在这里

于 2011-06-29T16:58:52.347 回答
1

http://localhost/something表示你正在调用 Something 控制器类的 index 方法

http://localhost/user/something表示您正在调用 User 控制器类中的 something 方法。

那有意义吗?

于 2011-06-29T15:28:35.023 回答
1

为了http://localhost/something工作,您需要一个使用方法调用的something控制器index。这与访问http://localhost/something/index.

或者,http://localhost/user/something意味着您有一个user控制器,其方法名为something.

这些帮助有用?

于 2011-06-29T15:28:58.493 回答
0

要从您的 URL 中删除 index.php,您必须使用此处描述的 mod_rewrite 方法

然后要从url中删除控制器名称(用户),您需要使用路由

在您的情况下,您将添加$route['^(something|something_else|etc)(/:any)?$'] = "user/$0";到您的 routes.php 文件中

于 2011-06-29T15:36:36.840 回答