3

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.

1
  • 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-]+~ Commented Nov 24, 2013 at 19:44

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

3 Comments

As far as using other routers, I checked out a few of them already but they were too complex for what I need.
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.
Let me add this very powerfull and easy-to-handle router: github.com/klein/klein.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.