Home > PHP > Django inspired SEF urls using PHP

Django inspired SEF urls using PHP

Update : Full code, including example usage can be downloaded here.

Over the last 5 years or so, I’ve been developing my own personal php CMS/Framework. Based loosely on Mambo / Joomla with bits and pieces thrown in whenever I found something interesting and inspiring.

After having worked with Django over the last few months I’ve made some significant changes to my framework – actually, I’ve pretty much re-written the entire core from the ground up. I love the way Django works so much, I just had to incorporate some of their ideas – something about mimicry being the ultimate flattery.

So anyway – one of the features I find most useful is the way urls are handled. Powerful, flexible but without sacrificing ease of use – what more could one ask for. I like the fact that it has a default method of handling the urls, but gives you the freedom to create your own conversion rules.

Ok so, mine doesn’t work in exactly the same manner, but the general idea is the same. I have a class file instantiated on every call to my gateway page (index.php). This class reads in the REQUEST_URI, and performs a regex match on an array of “url patterns”. Much like in Django – you have a url.php for each model in the framework which sets up the pattern array for that particular model. I also have a “failsafe” pattern array which gets used if the class cannot find the required url.php, or a match is not found.

Basically, the contents of one of these url.php files could look like the following :

$modelpattern = array(
array('@^/gallery/browse/(?P<category>\d+)[/]*$@', array("op"=>"browse")),
array('@^/gallery/browse[/]$@', array("op"=>"browse")),
array('@^/gallery/(?P.*)[/]*$@', "")

Each element of the array is a nested array containing two elements. The first element is the pattern to match while the second element is another array containing any additional parameters one would like to send to your model.

The other thing to note is the “(?P<category>\d+)” part in the example above. As you should probably know, preg_match allows you to read in parameters (the ?P bit) which you can use in your scripts.

So for instance, the url you’re trying to match is /gallery/browse/33 .

The following code loops through each element of $modelpattern and when a match is found, places any parameters we may have set into $matches.

foreach($modelpattern as $pattern){
    $matchFound = preg_match($pattern[0], $request, $matches);
    if ($matchFound) {
		foreach($matches as $key=>$value){
			if (is_string($key)){
				$_REQUEST[$key] = $value;
		if (is_array($pattern[1])){
			foreach ($pattern[1] as $key=>$value){
	return true;

What happens is that /gallery/browse/33 is matched with one of our patterns and the value “33” gets placed into $matches[“category”]. The above code then loops through the $matches array and adds each element to the $_REQUEST superglobal therefore $_REQUEST[“category”] now has a value of “33”). It then also loops through the second element of the matching array and loads those parameters into $_REQUEST – in this instance setting $_REQUEST[“op”] to “browse”.

It’s all pretty simple, and I’m sure there are huge improvements that can be made, but it works really well for what I need done. I’ll post the full source up if anyone’s actually interested 😉

Update : Full source code and example usage can be downloaded here.


Categories: PHP Tags: , , , ,
  1. November 10, 2008 at 2:18 am


    I am really interested, been looking since weeks for a good class
    Can you post or email the source please?


  2. calisza
    November 10, 2008 at 7:45 am

    Thanks for the comment. I’ll post the source up for you shortly.

  1. December 11, 2008 at 9:16 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: