Clean URLs are great! They add a touch of professionalism and elegance to your website and to your code. We are going to build a barebones clean URL sample that you can use in your next project. To begin, let’s take a look at the directory structure I will use.
You can use any directory structure you like by simply adjusting your autoloader.
Clean URLs
<?php //Autoload.php define('ROOT_PATH','/path/to/your/files'); function better_autoload($class){ $filename = ucfirst($class).'.php'; $subdirectories = array('View'); foreach($subdirectories as $subdir){ $full_filename = ROOT_PATH.'/'.$subdir.'/'.$filename; if(file_exists($full_filename)){ include_once($full_filename); }elseif(file_exists(ROOT_PATH.'/'.$filename)){//check in the root path include_once(ROOT_PATH.'/'.$filename); } } } spl_autoload_register('better_autoload');
This first file is simply the autoloader from here adjusted to fit our simplified structure.
<?php //TestView.php class TestView{ function func1(){ echo 'Function 1 found'; } function func2($a,$b,$c){ echo 'func2 with parameters a='.$a.',b='.$b.',c='.$c; } }
TestView is our sample view class. For our clean URL setup we will be reaching this class by navigating to mywebsite.com/test/func1 or mywebsite.com/test/func2
Options +SymLinksIfOwnerMatch RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
This is the .htaccess file, and the heart of our clean URL setup. This basically means that if a file exists, we will serve it; but if it doesn’t exist, then we will send the request to index.php. It is worth your time to learn more about what .htaccess can do for you.
<?php //index.php include_once('autoload.php'); $params = array(); $uri = $_SERVER['REQUEST_URI']; $paths = explode("/",$uri); foreach($paths as $k=>$p){ list($path,$x) = explode('?',$p,2); if($k==3){$page = $path;} if($k==4){$function = $path;} if($k>=5){$params[] = $path;} } $classview = ucfirst(strtolower($page)) . 'View';//the name of the class to be called e.g. SampleView if(class_exists($classview)){ $view = new $classview(); call_user_func_array(array($view,$function),$params); } ?>
Finally, this is the index file where all of our clean URLs are redirected to. This is where we process the URL that the user is trying to reach and try to find the correct file and function to run. The $paths variable created on line 6 is an array of “words” from the URL. We pick through these on lines 9-11 to find the $page, $function, and $params. Note the $x on line 8 contains the string of http query parameters (after the ? in the url). It is very important to adjust the values in the conditions on lines 9 through 11. In my example, I was calling this in a subdirectory so I had to push the path back farther. If your path is website.com/class/function/param1 for example then you will need to use $k==1 on line 9, $k==2 on line 10, and $k>=5 on line 11.
I am storing my “view” classes in files named SomethingView but I don’t want the word “view” in the URL so I add it on line 14.
This is all there is to making a very simple clean URL PHP application. When you get this set up and you want to make it more advanced, send me a comment with your questions.