Codeigniter url 路径
这是我提出这个问题的最简单的方法,因为我还没有完全理解发生了什么,或者我做错了什么。
我的网址有问题。
http://localhost/index.php/user
与相同 http://localhost/
但是 http://localhost/index.php/user/something
与不同 http://localhost/something
我该如何制作http://localhost/something
工作吗?
它是否必须是 http://localhost/user/something ,我该如何使其工作?
This is the most simple way I can ask this question as I have not fully understood what's going on, or what I am not doing right.
I'm having trouble with the url.
http://localhost/index.php/user
is the same as http://localhost/
buthttp://localhost/index.php/user/something
is not the same as http://localhost/something
How do I make http://localhost/something
work?
Does it have to be http://localhost/user/something
, how do I make that work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要了解 CodeIgniter 的 URL 是如何工作的。
URL 由一些段组成。
http://localhost/index.php/user/something/thing
在此示例中,user
、something
和thing
> 是 URL 的片段。URL 的各个部分指示将运行哪个控制器以及该控制器的哪个方法。
http://localhost/index.php/user/something/thing
在此示例中,调用user
控制器中的something
方法,并且thing
作为参数传递给该方法。URL 的第一段表示控制器。
URL 的第二段指示该控制器的方法。
以下段作为参数发送到该方法。
但有一些默认值。
如果您的 URL 是
http://localhost/index.php/something
,则您已将something
指定为控制器,但因为您没有指定任何方法,调用默认方法index
。所以上面的 URL 与http://localhost/index.php/something/index
相同
如果你的 URL 是
http://localhost/index.php/
code>,您没有指定任何段(没有控制器,也没有方法)。因此,在application\config\routes.php
中指定的默认控制器是加载的控制器。将调用该控制器的哪个方法?当然是index
方法。--您可以通过将
$route['default_controller'] = "site";
更改为application\config\routes.php< 中适合您的应用程序的内容来设置默认控制器/code> 的文件。
如果您希望
http://localhost/user/something
与http://localhost/index.php/user/something
相同,那么您有为您的应用程序创建自定义路由。有关该内容的更多信息此处。You need to understand how CodeIgniter's URLs work.
An URL consists of some segments.
http://localhost/index.php/user/something/thing
In this exampleuser
,something
andthing
are segments of the URL.Segments of the URL indicate which controller and which method of that controller will run.
http://localhost/index.php/user/something/thing
In this example the methodsomething
fromuser
controller is called andthing
is passed to that method as a parameter.The first segment of the URL indicates the controller.
The second segment of the URL indicates the method of that controller.
The following segments are sent to that method as parameters.
But there are some defaults.
If your URL is
http://localhost/index.php/something
, you havesomething
specified as the controller, but because you have not specified any method, the default method which isindex
is called. So the above URL is the same ashttp://localhost/index.php/something/index
If your URL is
http://localhost/index.php/
, you don't have any segments specified (no controller and no method). So the default controller which is specified inapplication\config\routes.php
is the loaded controller. Which method of that controller will be called? Of course theindex
method.--You can set the default controller by changing
$route['default_controller'] = "site";
to what ever fits your application inapplication\config\routes.php
's file.If you want
http://localhost/user/something
to be the same ashttp://localhost/index.php/user/something
, you have to create custom routes for your application. More info on that here.http://localhost/something 表示您正在调用 Something 控制器类的索引方法
http://localhost/user/something 表示您正在调用 User 控制器类中的 some 方法。
这有道理吗?
http://localhost/something indicates that you are calling the index method of the Something controller class
http://localhost/user/something indicates that you are calling the something method in the User controller class.
Does that make sense?
为了使
http://localhost/something
工作,您需要一个名为something
且具有index
方法的控制器。这与访问 http://localhost/something/index 相同。或者,
http://localhost/user/something
暗示您有一个user
控制器,其方法名为something
。这有帮助吗?
In order to make
http://localhost/something
work, you need a controller calledsomething
with anindex
method. This would be the same as accessinghttp://localhost/something/index
.Alternatively,
http://localhost/user/something
implies that you have auser
controller with a method calledsomething
.Does that help at all?
要从 URL 中删除 index.php,您必须使用 此处 描述的 mod_rewrite 方法,
然后删除控制器网址中的名称(用户),您需要使用 routes
在您的情况下,您将添加
$route['^(something|something_else|etc)(/:any)?$'] = "user/$0";
到你的routes.php 文件To remove index.php from your URL you have to use the mod_rewrite method described here
Then to remove the controller name (user) from the url, you need to use routes
In your case, you would add
$route['^(something|something_else|etc)(/:any)?$'] = "user/$0";
to your routes.php file