Reputation: 75
I Have some code :
<?php
switch (REQUESTED_URL) {
case '/home' :
require __DIR__ . '/views/Home.php';
break;
case '' :
require __DIR__ . '/views/Home.php';
break;
default:
header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
echo "Internal Server Error (Routes URL Not Found)";
exit();
break;
}
?>
How to use switch case function for dynamic url ? ex : /home/$1
Upvotes: 0
Views: 1455
Reputation: 958
You can't. switch
compares equality of the specified string to a list of values, just as if($string == $excepted_value) {}
(see documentation for the switch
control structure). It doesn't check whether a string matches a regex expression, so you can't check for a dynamic URL inside a switch
structure.
Instead, to support dynamic URLs (i.e. with query parameters), try to use inbuilt features of PHP, for example $_GET
for URL query parameters, $_SERVER['REQUEST_URI']
for the request URI, and regex expressions to extract specific parts of that URI.
Upvotes: 2