我们目前使用Switch case url配置来帮助我们在一些url上导航,我不确定是否有更简单的方法来做到这一点,但我似乎找不到1.
<?php if (! isset($_GET['step']))
{
include('./step1.php');
} else {
$page = $_GET['step'];
switch($page)
{
case '1':
include('./step1.php');
break;
case '2':
include('./step2.php');
break;
}
}
?>
现在这个系统工作得很好,但我们遇到的唯一障碍是如果他们输入xxxxxx.php?步骤=3 boom,他们只是得到一个空白页面,这应该是正确的,因为它没有处理"3"的情况,但我想知道的是……底部是否有PHP代码可以告诉它,除了这两种情况,它可以重定向回xxxxx。PHP ?
感谢丹尼尔以default
为例。也就是说,将开关更改为如下内容:
<?php if (! isset($_GET['step']))
{
include('./step1.php');
} else {
$page = $_GET['step'];
switch($page)
{
case '1':
include('./step1.php');
break;
case '2':
include('./step2.php');
break;
default:
// Default action
break;
}
}
?>
所有switch
语句都允许在没有其他情况下触发default
情况。类似…
switch ($foo)
{
case 1:
break;
...
default:
header("Location: someOtherUrl");
}
是可行的。但是,您可能想要搜索其他更健壮和可扩展的页面分发解决方案。
不如用另一种方法:
<?php
$currentStep = $_GET['step'];
$includePage = './step'.$currentStep.'.php'; # Assuming the pages are structured the same, i.e. stepN where N is a number
if(!file_exists($includePage) || !isset($currentStep)){ # If file doesn't exist, then set the default page
$includePage = 'default.php'; # Should reflect the desired default page for steps not matching 1 or 2
}
include($includePage);
?>