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 allowlocalhost/bonify?datasetID=1a2b3c4d5e6f7
to pass through to thebonify.php
? I'm guessing some kind of pattern match [0-9],['a'-'z'] throughpreg_match
? Or some kind of wildcard, but allowinglocalhost/bonify/split
not to be confused withlocalhost/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;
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.
Forlocalhost/bonify
:
$request = /bonify
And it matches the pattern.
Forlocalhost/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) {
// ....
}
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.