Welcome Guest, Not a member yet? Register   Sign In
Question about the url handling.
#1

[eluser]Unknown[/eluser]
In which file does CI do it's url handling. I'm new to this and I don't understand how there is no 404 error when calling like index.php/comments/1 from the video tutorial (comments being a method in the index class [which extends Controller]). This file doesn't exist, so I suppose somewhere in CI there is something that creates a new Index object and calls the comments method.

Can you post the sequence of actions for this? For example, something like:
After submitting an URL, class X recieves the request and creates the appropriate controller object. Then, method Y of the controller class is created, etc etc.

Thank you very much!
#2

[eluser]crumpet[/eluser]
i haven't looked at the code in too much detail but i believe that index.php just reroutes

So you call index.php/comments/1
it explodes that and gets each parameter
the first parameter it uses to figure out which controller to call - comments
it looks in the controllers directory for a file called comments.php
if it finds it then it sends it over there and attaches the parameter 1
usually this would be the name of the function to call in the controller however here i think theres an index function which looks like this:
Code:
function index($commentID){
  //find the comment and display it
}
so what happens is it defaults to the index function and the "1" becomes the parameter for it instead
so index takes 1 as the commentID and then displays that ID. If there wasn't an index function then the controller would look for the "1" function and run that.

basically
index.php/model/controller/param1/param2/etc...

also you can use the routes.php file in the config folder to make your own logic
so you can make index.php/X go anywhere you want
#3

[eluser]Pascal Kriete[/eluser]
Something is backwards in this thread. The url: index.php/comment/1 would be the comment class and the function 1. There is no index class, only an index function. You can, however, have a default controller (set in config/routes.php).

The standard url is: index.php/class/function/params
If only a class is given, the index function is used. If you're passing a parameter, you must include index in your url:
Code:
/ - calls default_controller::index()
/index.php/blog - calls blog::index()
/index.php/blog/comment/1 - calls blog::comment(1)
/index.php/blog/1 - calls blog::1() - fails

And since you asked, here's a basic rundown:
1. Request lands in index.php - file locations are worked out and it's passed on to system/codeigniter/CodeIgniter.php
2 CodeIgniter.php orchestrates the rest of the procedure, hence why it's know as the front-controller (index.php is the bootstrap file)

CodeIgniter.php:
Starting from the top (the relevant bits):
a. Includes a bunch of files, loads a bunch of core libraries...
b. Loads the URI class : this class handles splitting up the URI, most of the functions are called from the router
c. Loads the Router class:
    - Checks for routes (_set_routing)
    - Gets the uri string and splits it (_explode_segments)
    - Determines the controller (segment 1 or 2), if no segments are set it uses the default controller (_set_routing)
    - Checks for a second segment, if it doesn't exist, it's set to index (the index function) (_set_request / _validate_request)
    - Verifies that the controller file exists - if it doesn't, it shows a 404 (_validate_request)
d. Checks for a cache file
e. Includes the 'CI Base' file based on the PHP version
f. Includes the base controller
g. Instantiates the controller (which extends the base controller, which extends CI Base, therefore fully instantiating the 'CI Super Object')
h. Checks for a _remap function, or the requested function. If the function exists, calls it - else, shows a 404
i. After the controller executes, the output is sent

So there's the basics, might be fun to make a wiki entry. I skipped quite a bit to simplify it.
You might also find this interesting: where's that file coming from.
#4

[eluser]crumpet[/eluser]
o sorry i realized now my mistake
i've been working on a site where i had routed the the controller name to the index function so I didn't have to put /index/ before the parameters
sorry for the confusion




Theme © iAndrew 2016 - Forum software by © MyBB