3

我正在使用 PHP 来布局导航菜单并根据 URL 显示内容。

我已经到达确定要显示什么内容的 IF-ELSE 堆栈(通过加载必要的类和/或方法)。但是必须有更好的方法来写这个..有什么建议吗?

BreadCrumbs::getCrumb()是一种静态方法,用于根据索引值(URI 请求,用'/'分割,然后保存在数组中)从 URL 中检索保存的元素。

... (ArrayHelp::recValueSearch(BreadCrumbs::getCrumb(2), Config::getNavPrimary())根据包含来自 Config 类的导航列表的数组检查 URI 元素。

BreadCrumbs::setEmptyCrumb(1, "home")运行一个设置默认值的方法,如果该值不存在或无效(在导航列表数组中)

<?php
// set bread crumbs
BreadCrumbs::setCrumbs($_SERVER['REQUEST_URI']);
BreadCrumbs::setEmptyCrumb(1, "home");
BreadCrumbs::setEmptyCrumb(2, "all");



if (BreadCrumbs::getCrumb(1) == 'about') {
    echo 'This is the <b>About</b> Page';
}
else if (BreadCrumbs::getCrumb(1) == 'contact') {
    echo 'This is the <b>Contact</b> Page';
}
else if (BreadCrumbs::getCrumb(1) == 'search') {
    echo 'This is the <b>Search</b> Page';
}
else if (BreadCrumbs::getCrumb(1) == 'home') {
    if (BreadCrumbs::getCrumb(2) == 'all') {
        echo 'This is the <b>Home</b> Page';
    }
    else if (ArrayHelp::recValueSearch(BreadCrumbs::getCrumb(2), Config::getNavSecondary())) {
        echo 'This is the list page for category: <b>' . BreadCrumbs::getCrumb(1) . '</b> and the country filter is: <b>'. BreadCrumbs::getCrumb(2) . '</b>';
    }
    else {
        echo 'change filter value and go to the <b>home</b> page';
    }
}
else if (ArrayHelp::recValueSearch(BreadCrumbs::getCrumb(1), Config::getNavPrimary())) {
    if (BreadCrumbs::getCrumb(2) == 'all') {
        echo 'This is the list page for category: <b>' . BreadCrumbs::getCrumb(1) . '</b> and the country filter is <b>all countries</b>';
    }
    else if (ArrayHelp::recValueSearch(BreadCrumbs::getCrumb(2), Config::getNavSecondary())) {
        echo 'This is the list page for category: <b>' . BreadCrumbs::getCrumb(1) . '</b> and the country filter is: <b>'. BreadCrumbs::getCrumb(2) . '</b>';
    }
    else {
        echo 'change filter value to all and go to category: <b>' . BreadCrumbs::getCrumb(1) . '</b>';
    }
}
else {
    echo 'redirect page to home/all';
}

?>

编辑:更改为如下所示的 Switch 语句,更好一点..

    <?php
    $array_helper = new ArrayHelp;
    $valid_primary = $array_helper->recValueReturn(BreadCrumbs::getCrumb(1), Config::getNavPrimary());
    $valid_secondary = $array_helper->recValueReturn(BreadCrumbs::getCrumb(2), Config::getNavSecondary());
    switch (BreadCrumbs::getCrumb(1)) {
        case 'about' :
        case 'contact' :
        case 'search' :
            echo 'This is the <b>' . BreadCrumbs::getCrumb(1) . '</b> Page';
            break;
        case 'home' :
            switch (BreadCrumbs::getCrumb(2)) {
                case 'all' :
                    echo 'This is the <b>Home</b> Page';
                    break;
                case ($valid_secondary[1]) : 
                    echo 'This is the list page for category: <b>' . BreadCrumbs::getCrumb(1) . '</b> and the country filter is: <b>'. BreadCrumbs::getCrumb(2) . '</b>';
                    break;
                default:
                    echo 'change filter value and go to the <b>' . BreadCrumbs::getCrumb(1) . '</b> page';
                    break;
            }
            break;
        case ($valid_primary[1]) :
            switch (BreadCrumbs::getCrumb(2)) {
                case ($valid_secondary[1]) : 
                    echo 'This is the list page for category: <b>' . BreadCrumbs::getCrumb(1) . '</b> and the country filter is: <b>'. BreadCrumbs::getCrumb(2) . '</b>';
                    break;
                default:
                    echo 'change filter value and go to the <b>' . BreadCrumbs::getCrumb(1) . '</b> page';
                    break;
            }
            break;
        default:
            echo 'redirect page to home/all';
            break;
    }
?>
4

2 回答 2

5

是的,switch 语句更简洁一些

switch(BreadCrumbs::getCrumb(1)) {

   case 'about':
     echo 'This is the <b>About</b> Page';
     break;  

   case 'contact':
     echo 'This is the <b>Contact</b> Page';
     break;

   case 'search':
     echo 'This is the <b>Search</b> Page';
     break;


   // ....

   default: 
     // Default behavior
      break;
}

下一步是获取您的每个页面打印输出并将其放置在自己的函数中或将 html 外部化到模板文件中。

于 2012-12-22T20:23:46.897 回答
2

如果您的示例代码显示了您的完整要求,其中一些可以通过使用返回的实际字符串大大简化getCrumb

  $crumb = ucfirst(BreadCrumbs::getCrumb(1));
  switch($crumb){

     case 'home': case 'foobar': # etc, etc
        echo "This is the <b>$crumb</b> Page";
        break; # or add some extra logic
     default:
        # handle invalid crumbs here
        break;
  }

(ucfirst用于使字符串的第一个字符大写。)

于 2012-12-22T20:29:21.490 回答