learn

routing with php

category: Ask
Created by: Dr.Ferrous

routing with php

category: Ask
Created by: Dr.Ferrous
twitter google+ facebook pin 

Suppose a website is http://site.com
Now if I want to check if http://site.com/search/ or http://site.com/Something/
such kind of links available or not what will be the code?
In a nutshell : The code will check for some inputs given by me in a array and it will ask for a site link to input and It will add the items of the array and check if the site has that link!
Hope you Understood!!

by Fuad Kibria

 

Your question is regarding routing with php and used in most websites nowadays, in order to make this done on your apache server, please apply the following steps:

Rewrite Engine

When you open a url (like http://www.phpwin.org/posts/hello.php?id=5) apache will try to read the file "hello.php" in folder "posts" and send it to the browser.

We don't want Apache to do that! so we will have to create a file ".htaccess" and put it in the root directory, with the following lines:

if you already have ".htaccess" in the root, just append those lines at the end of the file.

 

RewriteEngine On tells apache to begin rewriting our new custom rules.


RewriteCond %{SCRIPT_FILENAME} !-f means that our new rules applies only for files that do no exist in server(such as http://www.phpwin.org/posts/hello.php?id=5)


RewriteCond %{SCRIPT_FILENAME} !-d means that our new rules applies for folders that do no exist in server (such as http://www.phpwin.org/posts/sub/)


RewriteRule ^.*$ index.php means that any requested uri (files & folders) will open index.php

 

For example, if the user opened "http://www.phpwin.org/posts/subs/hello.php", apache will actually execute this url:

http://www.phpwin.org/index.php

these rules work only on files & directories that do not exist. For example, if you a file img.png and the user opened http://www.phpwin.org/img.png, apache will not use our custom rules on it.

Catching uri with php

Now we have index.php to handle all of the requested URIs. Our next step is to read the requested uri with php.

Open index.php and add the following script:

Create a folder in root directory and name it "pages" and use it to put your pages inside it, such as index.php, portfolio.php & search.php...

The first line reads the requested uri & splits it by "/". For Example "http://www.phpwin.org/hello/world" will be saved in $req as array ["hello","world"]

The last 3 lines are to include header.php, $page & footer.php all the time because they are common, so when the user opens any page, he will always see a header & footer (you can change that if you want).


$page var is controlled by the requested uri using if conditions, for example, when the user opens http://www.phpwin.org/portfolio, file "pages/p.php" will be displayed.

And when he searches for something such as http://www.phpwin.org/search/text, file "pages/res.php" will be displayed (you can access the second argument "text" with $req[1])

If you want to check multiple arguments together such as http://www.phpwin.org/people/john/, you can use strpos


You can also make subs as long as you want, for example: http://www.phpwin.org/search/array/sortbydesc/filterbyuser/etc, all them can be accessed in $req array.


And finally, if $req[0] doesn't exist, page will be "pages/404.php"

This is exactly the same way I am using in phpwin.org, I hope this answers your question.