All,
I’m building a front controller in PHP. In it, I say:
if (isset($_GET['action'])){
$action=$_GET['action'];
} else {
$action='';
}
I follow that with a switch statement than governs which controller is called based on the value of $action:
switch ($action){
case '':
require_once('Controller_Welcome.php');
$command=new controller_Welcome();
break;
case 'logon':
require_once('Controller_Logon.php');
$command=new controller_Logon();
break;
default:
require_once('Controller_Unknown.php');
$command=new controller_Unknown();
break;
}
$command->execute();
This works fine. When I launch the app, the URL is http://.../Index.php? and the Controller_Welcome.php is called. If I click on the logon menu entry, I get http://.../Index.php?action=logon, and the Controller_Logon.php is called. If I manually edit the URL to set ...?action=... to some unknown value, I get Controller_Unknown.php, which is my error page. So all is well.
What I don’t understand is that if I manually alter the url to show http://.../Index.php?action=, I get the error page rather than the welcome page. Why is it that php doesn’t associate a url ending with ...?action= with the switch case $action='';?
(There’s no logical user case when that would happen, but I still don’t understand it…)
Thanks,
JDelage
PS: Var_dumping $action returns string(0) "".
When you set
?action=, you will get anullreturn value for$_GET['action']. So in your scenario, the switch statement will use the default case. Like everyone said, you can always use var_dump to see the return value.