[英]PHP - Why is “Index.php?action=” not recognized as “Index.php?”
All, 所有,
I'm building a front controller in PHP. 我正在用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
的值调用哪个控制器:
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?
当我启动应用程序时,URL是
http://.../Index.php?
and the Controller_Welcome.php
is called. 并调用
Controller_Welcome.php
。 If I click on the logon menu entry, I get http://.../Index.php?action=logon
, and the Controller_Logon.php
is called. 如果我单击登录菜单项,我会得到
http://.../Index.php?action=logon
,并调用Controller_Logon.php
。 If I manually edit the URL to set ...?action=...
to some unknown value, I get Controller_Unknown.php
, which is my error page. 如果我手动编辑URL以将
...?action=...
为某个未知值,我会得到Controller_Unknown.php
,这是我的错误页面。 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. 我不明白的是,如果我手动更改URL以显示
http://.../Index.php?action=
,我会得到错误页面而不是欢迎页面。 Why is it that php doesn't associate a url ending with ...?action=
with the switch case $action='';
为什么php没有将一个url结尾与
...?action=
与switch case $action='';
? ?
(There's no logical user case when that would happen, but I still don't understand it...) (当发生这种情况时,没有合乎逻辑的用户案例,但我仍然不理解......)
Thanks, 谢谢,
JDelage JDelage
PS: Var_dumping $action
returns string(0) ""
. PS:Var_dumping
$action
返回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']
. 当你设置
?action=
,你会得到$_GET['action']
的null
返回值。 So in your scenario, the switch statement will use the default case. 因此,在您的方案中,switch语句将使用默认情况。 Like everyone said, you can always use var_dump to see the return value.
像所有人说的那样,你总是可以使用var_dump来查看返回值。
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 这个工作正常,并抛出Controller_Welcome空行动
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.