Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to create a very simple URL routing, and my thought process was this:

  • First check all static URLs
  • Then check database URLs
  • Then return 404 if neither exists

The static URLs are easy to do of course, but I'm trying to figure out the best way to do dynamic ones. I would prefer not to have to set a static prefix, despite knowing that it would make this a lot easier to code.

This is what I currently have:

$requestURL = $_SERVER['REQUEST_URI'];

if ($requestURL == '/') {
    // do stuff for the homepage
}

elseif ($requestURL == '/register') {
    // do stuff for registration
}

// should match just "/some-unique-url-here"
elseif (preg_match("/([\/A-Za-z0-9\-]+)/",$requestURL)) { 
    // query database for that url variable
}

// should match "/some-unique-url/and-another-unique-url"
elseif (preg_match("(^\/[A-Za-z0-9\-]+\/[A-Za-z0-9\-]+)/",$requestURL)) {
    // query database for first and second variable
}

else {
    // 404 stuff
}

My problem is that if I have "/register" URI, it will match the second elseif statement as well as the regex statement. But I want to avoid having to specifically exclude each static URL from regex statement, such as this:

// should match just "/some-unique-url-here"
elseif ((preg_match("/([\/A-Za-z0-9\-]+)/",$requestURL)) &&
    ($requestURL !== '/register') &&
    ($requestURL !== '/')) { 
    // query database for that url variable
}

What's the easiest way to solve this problem? I'll probably have like 15-20 static URLs, so specifically excluding all of them would be very clunky.

share|improve this question
1  
Side note: there is no need to escape a hyphen - at the end of a character class (or at the beginning). Also to prevent some confusions, you might use other delimiters than forwardslash /, that way you won't need to escape them in your expression ~[/A-Za-z0-9-]+~ –  HamZa Nov 24 '13 at 19:44
    
@HamZA, cool thanks. Didn't know that. –  zen Nov 24 '13 at 19:50
add comment

1 Answer

up vote 3 down vote accepted

Your problem does not exist. If the first elseif ($requestURL == '/register') matches, all subsequent elseifs on the same level won't get evaluated.

You're already doing it right, just make sure you do the string comparisons (==) first.

On another note, don't reinvent the wheel.

share|improve this answer
    
Ahh duh! Stupid me, you are right! –  zen Nov 24 '13 at 19:52
    
As far as using other routers, I checked out a few of them already but they were too complex for what I need. –  zen Nov 24 '13 at 19:52
    
Multiple reasons to create your own. Companies want to see what you can do and don't care much if you can use someone else's work. Also its a great learning experience to create your own. In this way you'll be able to figure out your mistakes and maybe even find ways to speed up the script for your own needs etc. –  NSaid Feb 17 at 5:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.