PHP - Why is "Index.php?action=" not recognized as "Index.php?"
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) ""
.
Just a note that may help with readability, and further development efforts. It appears your naming convention could permit a bit more "magic", giving way for a sort of convention over configuration, and avoidance of code duplication:
define('PATH_CONTROLLERS', 'path/to/controllers/');
$action = !empty($_GET['action'])
? $_GET['action']
: 'default';
switch($action){
case 'default':
case 'welcome':
case 'authenticate':
$controller_name = "controller_{$action}";
break;
default:
$controller_name = 'controller_404';
break;
}
require PATH_CONTROLLERS . "{$controller_name}.php";
$controller = new $controller_name();
$controller->execute();
Given:
// index.php
$action: 'default'
$controller_name: 'controller_default'
require(): 'path/to/controllers/controller_default.php'
// index.php?action=authenticate
$action: 'authenticate'
$controller_name: 'controller_authenticate'
require(): 'path/to/controllers/controller_authenticate.php'
// index.php?action=foobar
$action: 'foobar'
$controller_name: 'controller_404'
require(): 'path/to/controllers/controller_404.php'
When you set ?action=
, you will get a null
return 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.
I think action is not what you think it is. This works as expected for me.
for url ending in 'action=' blank is echoed
for url ending in 'action=anything' anything is echoed
var_dump($_GET);
$action = $_GET['action'];
switch ($action){
case '':
echo "blank";
break;
default:
echo $action;
break;
}
The behavior you describe cannot be reproduced. I used the following and the results do no demonstrate what you are describing:
<pre>
<?php
if (isset($_GET['action'])){
$action=$_GET['action'];
} else {
$action='';
}
var_dump($action);
echo "\n";
var_dump($_GET);
echo "\n";
switch ($action){
case '':
die('empty action</pre>');
case 'logon':
die('logon action</pre>');
default:
die('unknown action</pre>');
}
?>
call:
http://host.com/test.php?action=
result:
string(0) ""
array(1) {
["action"]=>
string(0) ""
}
empty action
There is something with your other code.
this one works fine and throws Controller_Welcome at empty action
精彩评论