Home /PHP /The Best way to do URL Routing simply in PHP /

The Best way to do URL Routing simply in PHP

2:17 am, April 23, 2017

This probably should be titled The Best way to do URL Routing simply in PHP In my opinion. They key words are simply and best. I think i have simply and best covered for my usage. Your usage of these methods might totally differ but for me i have found that this way works for me most easily. Maybe i should call it, the best way that i have found to easily do routing in PHP with no extra server configuration needed. ;)

I know there are a lot of routing libraries out there for PHP i have tried a few of them and they are just too heavy for my usage. 

Ok lets sart with What is URL Routing? Its how you distinguish between pages in php. So say you have index.php and about.php for each page, that is routing at its very basic level. 

I tried for a while to re-write url's using php so that you dont get any of the ?=/ whatever stuff, but decided that after moving from server to server and changing different environments that i was writing something for localhost and then something else for the server which got very annoying.

PHP Link Class

I was detecting local host IP or server name and then changing the url's and i even made a class for it. Which was quite complex as i had just started writing classes and was not really the best at it, you can view it here. I dont use it any more as it just added a level of complexity that was not really needed, every time i created or needed a page link i would have to send it to that class to generate the link and as the class was not very well written it just made things a lot more tricky.

Javascript #'s

Before i tried the hard php method i did tinker with some javascript # tag loading. Which can be nice and quick to load, but i think also a bit messy after a while as each page is relying heavily on either javascript or jquery which is not really great in my opinion, what happens if you have a text browser like links or lynx (maybe they support javascript, i cant remember now) but i would say that the page would just break so not really a great method either. The only site i ended up using this on was a simple garage sale listing site. Which loads nice and fast due to the # watching, however its pretty limited and will not work unless it has access to jquery and javascript which is a bit annoying.

The other bad thing about using this method is that every url has to have some kind of javascript on it just to show the page content unless you fall back to php, which you can do. But i ended up not bothering with this method as it just got too complex for what i was using it for. 

Just Basic PHP

What i ended up doing with this site and others from now on relies on just sending one variable to check in the php $_GET request. 

The variable as you can see from this page and most pages on this site is

$p = $_GET['p'];

To make it still look like mostly a nice url i just pass the whole string and split it. so you end up with something like this : http://site.com/?p=/page-name/page-id/page-nice-name/

Now this can probably be a dangerous way to use one variable, as there are some limits on the length, however for what i am using it for i found that this limit so be fine for me. Some versions of php (which i dont use i think the php with suhosin) is limited to 512 characters per get request. Which is still quite a lot! Plus the page-nice-name section is more for seo and i dont usually use it anyway so all good!

Here is how i break it down, just so its easy to remember pretty much and it works every time for me. You just cant have any / in the page name or variable or it will break.

/* Check if p is set. */
$p = ""; if(isset($_GET['p'])) { $p = $_GET['p']; }

Explode $p into an array $pa on each, i also manually assign the array into variables i know this could be done better with some kind of loop but this is how i have done it for the moment!

$pa = explode("/",$p); 
$p1 = ""; if(isset($pa[1])) { $p1 = $pa[1]; }
$p2 = ""; if(isset($pa[2])) { $p2 = $pa[2]; }
$p3 = ""; if(isset($pa[3])) { $p3 = $pa[3]; }
$p4 = ""; if(isset($pa[4])) { $p4 = $pa[4]; }

I find this method simple and effective.

Then all you have to do is check for the $p matching your string requests with the following.

if(!$p > "") {	// home page	include("home.php"); // show the home page as $p is not set
if(file_exists("$p1.php")) {	
include("$p1.php"); // include the file name if it exists } else {
include("missing.content.php"); // show a missing content page. }

This will check for $p being set to something and if it is not it will show the contents of home.php

If the p name matches the file name of the same kind it will load that, making your code much easier as you can use one file for each page and it will just auto load it if the file exists. if the file does not exist it will show a page error.

When i first set this up i had one massive page.php file and just kept adding to it, but once you have about 10 pages it just becomes too hard to handle, and if you stuff up the page it can stuff up all of your pages at once.

Splitting up the pages into separate files makes it much easier to find and edit things and if you stuff up one of the pages or have an error on it it wont totally break your site rather just the page that you are working on.

Keep it simple!