configuring url routes php with wildcard, checking for GET_ data

I'm trying to understand what is going wrong here. I'm using php's routes through a switch.

localhost/bonify redirects to the correct place.

localhost/bonify?datasetID=1a2b3c4d5e6f7 redirects to the 404.

http://localhost/bonify/split goes to the correct place.

localhost/bonify/split?datasetID=1a2b3c4d5e6f7 redirects to the 404.

So how do I allow localhost/bonify?datasetID=1a2b3c4d5e6f7 to pass through to the bonify.php? I'm guessing some kind of pattern match [0-9],['a'-'z'] through preg_match? Or some kind of wildcard, but allowing localhost/bonify/split not to be confused with localhost/bonify.

Perhaps the ? should be part of the pattern match? I hope that makes sense?

 $request = $_SERVER['REQUEST_URI'];

 switch ($request) {
     case '/' :
         require __DIR__ . '\bonify'. $version . '\home.php';
         break;
     case '' :
         require __DIR__ . '\bonify'. $version . '\app\bonify.php';
         break;
     case '/bonify' :
         require __DIR__ . '\bonify'. $version . '\app\bonify.php';
         break;

     case '/bonify/split' :
         require __DIR__ . '\bonify'. $version . '\app\bonify_split.php';
         break;

Answer

Solution:

The switch statement looks for a string identical to the pattern. The easiest way to find out why your statement does not work is to print the $request variable out and compare it with your cases.

For localhost/bonify:

$request = /bonify

And it matches the pattern.

For localhost/bonify?datasetID=1a2b3c4d5e6f7:

$request = /bonify?datasetID=1a2b3c4d5e6f7

"/bonify?datasetID=1a2b3c4d5e6f7" !== "/bonify"

That does not match any pattern. You don't have a default case in your switch statement so you get 404.

The only way I see is to use preg_match() function in your cases (with a correct regex) to cover all possible options.

Edit: If you don't need the ID from the URL in that part of the code, you can just cut all characters from the question mark (substr() method) and save the result to the new variable. Then just use the new variable in your switch statement. It would also work.

 $request = $_SERVER['REQUEST_URI'];
 $cutRequest = strpos($request, "?") ? explode("?", $request)[0] : $request;
 switch ($cutRequest) {
 // ....
 }

Source