开发者

Codeigniter url path

开发者 https://www.devze.com 2023-03-16 11:17 出处:网络
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.

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/ but http://localhost/index.p开发者_高级运维hp/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?


You need to understand how CodeIgniter's URLs work.

  • An URL consists of some segments. http://localhost/index.php/user/something/thing In this example user, something and thing 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 method something from user controller is called and thing 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 have something specified as the controller, but because you have not specified any method, the default method which is index is called. So the above URL is the same as http://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 in application\config\routes.php is the loaded controller. Which method of that controller will be called? Of course the index method.

    --You can set the default controller by changing $route['default_controller'] = "site"; to what ever fits your application in application\config\routes.php's file.

  • If you want http://localhost/user/something to be the same as http://localhost/index.php/user/something, you have to create custom routes for your application. More info on that here.


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?


In order to make http://localhost/something work, you need a controller called something with an index method. This would be the same as accessing http://localhost/something/index.

Alternatively, http://localhost/user/something implies that you have a user controller with a method called something.

Does that help at all?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号