dongqiao3833 2012-06-12 14:14
浏览 56
已采纳

用于改变URL的PHP​​交换机案例

I'm building a site on the CodeIgniter framework and I'm using PHP switch to load the specific javascript relevant to the page. My issue comes when I get to the 3rd URI segment, which is generally a variable. for instance I have no issue with say

case 'foo/bar':

but what if my url was something like http://mysite.com/foo/bar/1234, where the 1234 if a variable passed to the controller. It obviously wouldn't make sense to write out a case for every single variable because there's about 30k right now.

currently here's a working snippet of my code...

switch( $this->uri->uri_string() ) {

            case '': 

            break;

            case 'browse':

            break;

            case 'contest/leaderboard':
  • 写回答

2条回答 默认 最新

  • dqc3469 2012-06-12 14:31
    关注

    You could also explode the url on the forward slash and then check each element of an array.

    for example

    $url_parts = explode('/', $this->uri->uri_string());
    switch($url_parts[0]){
      case 'foo':
         if(count($url_parts) > 1){
           if($url_parts[1] == "bar"){
             // when the url begins with foo/bar
           }
         }
         break;
       case 'browse':
         //when url is browse
       break;
    }
    

    The benefit of this is you could include code common to all urls that begin with foo. Not sure if this is required but could be useful possibly

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法