Sunday, February 20, 2011

Kopa teaches PHP: URL routing done simple

URL routing is to have an url called something like "kopasite.net/records/pack/TKT/" instead of "kopasite.net?records.php?pack=TKT", using somewhat same principle as the $_GET variables to include pages. It can be done pretty complicated, but in the end it's just a matter of dividing the url into a number of variables that you use to include pages ect.

To do URL routing modrewrite needs to be turned on in your PHP installation.

First off you need to add some stuff to your .htaccess file.
RewriteEngine on
RewriteRule ^ajax - [L]
RewriteRule !\.(js|ico|txt|gif|jpg|png|css)$ index.php
 First line turns the rewriting on. Second line makes an exception that the folder ajax will not be used for rewriting. I'm doing that as I have an actual folder called ajax with php files in it, that I otherwise will not be able to call with ajax functions as it would redirect them to index.php. Third lines redirects any files except the file types mentioned to your index.php file. So that means whatever link you make on your site it will redirect to index.php.

When it does that you can in your index.php file make an include that will include another .php according to the url.

So in your index.php or some route.php that is included in index.php you make the following code:
$already_there = str_replace('index.php', '', $_SERVER['SCRIPT_NAME']);
$slightly = str_replace($_SERVER['SCRIPT_NAME'], "", $_SERVER['REQUEST_URI']);
$uri = str_replace($already_there, '', $slightly);
This part does some manipulation with the url and gives a string with anything after your domain name, the part that will decide which page to include.

if($uri != ""){
    $pieces = array_filter(explode("/", $uri));
    if(count($pieces) == 3){
        $s = $pieces[0];
        $p = $pieces[1];
        $t = $pieces[2];
    }elseif(count($pieces) == 2){
        $s = $pieces[0];
        $p = $pieces[1];
    }elseif(count($pieces) == 1){
        $s = $pieces[0];
    }
}
This parts sets up to three variables that you can use to include pages or do other stuff with, like you'd use $_GET values. First line checks if the string is empty, if it is we don't set any variables ofcourse. Second line divides the url into an array, dividing by /'s, as well as removes any empty values from the array, which can happen if the url ends with an /. You can also use any other symbol like a dot or whatever if you like. You could stop now and use the variables $pieces[0] ect. in your scripts. I don't want to have them in an array so I do some more. The next if else part makes sure we don't set any values if we have nothing to put into them. It simples makes the variables $s for section, $p for page and $t for tab in that order if they exists, which I in turn use to include other pages.

For example on index.php I have this code:
$sections = array("records", "news", "info", "contest", "download", "community", "tools");
      if(in_array($s, $sections)){
              include "$s.php";
      }
First line is the different sections I have on my site, which I use to make sure it doesn't include a page that doesn't exist. Next it checks if $s exists in this array I made and then include a php file with that name.

And that's it.

There is just one more thing to remember. When you are on the url "kopasite.net/records/pack/TKT/" for example it will still think you're inside a folder. So if you have any pictures or css files ect. you need to set an absolute link to these. Like:
<img src="/images/logo.png">
Instead of:
<img src="images/logo.png"> 

2 comments:

Virus said...

good post, pls more like this.
btw im virus :P

Kopaka said...

I would recommend looking into using a framework instead though, which usually have behaviour like this built in. I use codeigniter.