2011-08-28 58 views
3

全部,PHP - 为什么“Index.php?action =”未被识别为“Index.php?”

我正在用PHP构建一个前端控制器。在这里面,我说:

if (isset($_GET['action'])){ 
     $action=$_GET['action']; 
    } else { 
     $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(); 

这工作得很好。当我启动应用程序时,URL是http://.../Index.php?,并调用Controller_Welcome.php。如果我点击登录菜单项,我会得到http://.../Index.php?action=logon,并调用Controller_Logon.php。如果我手动编辑URL以将...?action=...设置为某个未知值,我会得到Controller_Unknown.php,这是我的错误页面。所以一切都很好。

我不明白的是,如果我手动更改网址以显示http://.../Index.php?action=,我会得到错误页面而不是欢迎页面。为什么php不会将以...?action=结尾的网址与开关盒$action='';关联?

(还有时会发生没有逻辑上的用户情况,但是我还是不明白......)

感谢,

JDelage

PS:Var_dumping $action回报string(0) ""

+0

的var_dump($行动);回报什么? – 2011-08-28 04:05:45

+0

尝试var_dumping你的变量,也许这不是你想象的那样。 –

+0

它返回:'string(0)“”' – JDelage

回答

1

当您设置?action=时,您将获得$_GET['action']null返回值。所以在你的场景中,switch语句将使用默认情况。就像大家所说的,你总是可以使用var_dump来查看返回值。

+1

1 )GET和POST值永远不会是'null',最坏的情况是它们是空字符串! 2)'null ==''' – deceze

+1

开关不做类型检查,它相当于if(null ==''){...}'在PHP的常规非类型比较中评估为true 。 –

0

我认为行动不是你想象的那样。这符合我的预期。

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; 
    } 
+0

看到您关于倾销var的最新评论。我逐字复制了你的代码,你的switch语句适合我。有没有什么方法可以让你的'Controller_Welcome'出错,然后重定向到'Controller_Unknown'? – mrtsherman

2

只是一个说明,可能有助于可读性和进一步的开发工作。看来你的命名约定可能允许多一点“魔术”,为一种约定优于配置的让位和代码避免重复:

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(); 

考虑:

// 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' 
+0

三元将永远不会有助于可读性 –

+2

@Col。 Shrapnel - *嵌套的*三元运算符会发出一个解析错误,如果我有我的方式,但你是否建议这实际上*少*可读? – Dan

+1

我说的不是解析错误,而是可读性。 '如果'陈述总是可以看作简单的英语,而三元是野蛮的。 '$ action =!empty($ _ GET ['action']'my foot!最欺骗的代码可能会写出 –

-1

有与您的其他代码一起。
这一个工作正常,并抛出Controller_Welcome在空行动

0

你描述的行为不能被复制。我用下面的,结果也没有表明你所描述的:

<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>'); 
    } 
?> 

电话:

http://host.com/test.php?action= 

结果:

string(0) "" 

array(1) { 
    ["action"]=> 
    string(0) "" 
} 

empty action 
+0

你有点迟了答案 –

+0

阅读答案(及其时间)我明白你的意思;) –

相关问题